What input reading functions are there in C?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2006
Posts: 999
Reputation: EnderX is an unknown quantity at this point 
Solved Threads: 1
EnderX EnderX is offline Offline
Posting Shark

What input reading functions are there in C?

 
0
  #1
May 29th, 2007
I've been attempting to teach myself some C, as I have been tasked with maintaining a program written for a handheld barcode scanner that appears to be written in this language. (At least, I'm assuming that for the moment, considering the fact one of the first statements I saw when I started skimming the source code was a malloc).

I've decided that for my first test program (to be shifted to the scanner to see if I can get it to work there) should be a four-function calculator. The scanner has a numeric keypad setting, so the numbers and symbols shouldn't be any problem.

My problem here is a bit simpler. So far, the only input reading method I've found in the text I'm using for this is the getchar() function. From what I can see, that doesn't look as though it's going to be able to handle more than one input character at a time; ergo, any mathematical equation involving numbers 10 and up would be (as far as I can see) impossible to directly deal with.

What other methods of input reading (from command line; I'm assuming that's stdin) are there? Is there something designed to read in multiple characters, or do I need to look into developing a read/store loop pattern for my numbers?

All input highly welcomed.
"No trees were harmed in the production of this post. However, several electrons were severely inconvenienced."

Kumquat.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: What input reading functions are there in C?

 
0
  #2
May 29th, 2007
>any mathematical equation involving numbers 10 and up would be (as
>far as I can see) impossible to directly deal with.
You can perform any kind of input using single character input. In fact, that's basically what all of the other input functions do: they read one character at a time, store it somewhere, and optionally do some kind of formatting. For example, let's say you want very basic integer input:
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int getint ( void )
  5. {
  6. char buff[BUFSIZ];
  7. int result = 0;
  8. int ch;
  9.  
  10. /* Pull all digits and add them to the result value */
  11. while ( ( ch = getchar() ) != EOF && isdigit ( ch ) ) {
  12. result = 10 * result + ( ch - '0' );
  13. }
  14.  
  15. /* Return the unconverted character to the stream */
  16. ungetc ( ch, stdin );
  17.  
  18. return result;
  19. }
However, most of the time you don't need to do that. Not to mention that it takes a great deal of skill and effort to do it properly. For example, you should never use the above function in real code because it lacks decent bulletproofing. In your case, you should look up the fgets function for reading a line of input. That will give you a string that you can then parse. You can parse it with sscanf, or a number of functions from string.h.
Last edited by Narue; May 29th, 2007 at 12:14 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: What input reading functions are there in C?

 
0
  #3
May 29th, 2007
And don't try to shortcut the fgets()/sscanf() pair with scanf(). If someone suggests it, stare at him in horror and cast the daemon away! Here's why. Read the entire scanf() series...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,035
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: What input reading functions are there in C?

 
1
  #4
May 29th, 2007
A little example of using what it has been said so far.
Sorry, the proper c tagging is not working at this time.

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int getinteger( int *result )
  5. {
  6. int send = 0;
  7. char ch;
  8. char buffer [ 13 ] = { '\0' };
  9. if( fgets( buffer, sizeof buffer, stdin ) && !isspace( *buffer ) && sscanf( buffer, "%d%c", result, &ch ) == 2 && ( ch == '\n' || ch == '\0' ) )
  10. {
  11. send = 1;
  12. }
  13. else
  14. {
  15. if( buffer[ strlen( buffer ) - 1 ] != '\n' ) /* if newline is not found in string */
  16. {
  17. while( getchar() != '\n' ); /* clear stdin */
  18. }
  19. send = 0;
  20. }
  21. return send;
  22. }
  23.  
  24. int main( void )
  25. {
  26. int number = 0;
  27.  
  28. do
  29. {
  30. fputs( "Enter an integer: ", stdout );
  31. fflush( stdout );
  32. }
  33. while ( !getinteger( &number ) );
  34.  
  35. printf( "number = %d\n", number );
  36.  
  37. getchar();
  38. return 0;
  39. }
Last edited by Aia; May 29th, 2007 at 8:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What input reading functions are there in C?

 
0
  #5
May 30th, 2007
Originally Posted by Aia View Post
Sorry, the proper c tagging is not working at this time.
would you please explain what you mean by that? you can use [ code=c ] if you want to see line numbers, but I assume you already know that. If something is broken maybe you should start a thread in DaniWeb Community Feedback.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,035
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: What input reading functions are there in C?

 
0
  #6
May 30th, 2007
I try to use the [code=C] tagging way like I always use, but this time
didn't work not matter what I try. Here I attached a snap-shot I took of it.
I will do like you say and start a thread in the Community Feedback.
Last edited by Aia; May 30th, 2007 at 6:09 pm.
Attached Thumbnails
wrong c tagging.JPG  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What input reading functions are there in C?

 
0
  #7
May 30th, 2007
I just tried it and it was ok -- If it persists for you please report it to Dani so that she can fix it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,047
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: What input reading functions are there in C?

 
0
  #8
May 30th, 2007
There was a bug yesterday. It was fixed early this morning.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: What input reading functions are there in C?

 
0
  #9
May 31st, 2007
Originally Posted by Aia View Post
  1. if( fgets( buffer, sizeof buffer, stdin ) && !isspace( *buffer ) && sscanf( buffer, "%d%c", result, &ch ) == 2 && ( ch == '\n' || ch == '\0' ) )
That's an unreadable and grotesque line of code.... Yuck! It certainly can be make much more readable and less convoluted.

Could you try to clean it up? Remember, these are new programmers and showing an example that is too complex for them to understand is simply useless to them...
Last edited by WaltP; May 31st, 2007 at 12:54 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: What input reading functions are there in C?

 
0
  #10
May 31st, 2007
>Remember, these are new programmers and showing an example that is
>too complex for them to understand is simply useless to them...
Not to mention that no self-respecting programmer would be caught writing that. It's harder to follow the logic, harder to maintain, and harder to document. Don't try to do too much in the loop condition.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC