944,083 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8229
  • C RSS
Apr 19th, 2007
0

non blocking getchar.. big problem :(

Expand Post »
hi,
does anyone knows if is there a way to make getchar non blocking?

I'm using curses and I'm on linux platform. With a thread version of the same program I have solved my problems with getch and with the timeout function but now the program doesn't work properly and I must use
  1.  
  2. c=getchar();
  3. if (c==0) c=getch();
Using timeout(1) makes non blocking only the c=getch part of this code but not the getchar one.

I have tried to solve this problem using this function
  1. int kbhit(void)
  2.  
  3. {
  4.  
  5. struct timeval tv;
  6.  
  7. fd_set read_fd;
  8.  
  9. tv.tv_sec=0;
  10.  
  11. tv.tv_usec=0;
  12.  
  13. FD_ZERO(&read_fd);
  14.  
  15. FD_SET(0,&read_fd);
  16.  
  17. if(select(1, &read_fd, NULL, NULL, &tv) == -1)
  18.  
  19. return 0;
  20.  
  21. if(FD_ISSET(0,&read_fd))
  22.  
  23. return 1;
  24.  
  25. return 0;
  26.  
  27. }

and this code
  1. if (kbhit())
  2. c = getchar();
but the program doesn't work properly...

Does anyone knows a solution?

I'm 5 days in a row searching of solve this big problem unhappy
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kazuyaz is offline Offline
4 posts
since Apr 2007
Apr 19th, 2007
0

Re: non blocking getchar.. big problem :(

does anyone has a idea?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kazuyaz is offline Offline
4 posts
since Apr 2007
Apr 19th, 2007
0

Re: non blocking getchar.. big problem :(

The solution is not to use functions that compromise portability.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 19th, 2007
0

Re: non blocking getchar.. big problem :(

Click to Expand / Collapse  Quote originally posted by iamthwee ...
The solution is not to use functions that compromise portability.
yes, getchar() is portable however..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kazuyaz is offline Offline
4 posts
since Apr 2007
Apr 23rd, 2007
0

Re: non blocking getchar.. big problem :(

no one?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kazuyaz is offline Offline
4 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help Please
Next Thread in C Forum Timeline: How to overload + operator for STL Vectors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC