944,111 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5703
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 29th, 2007
0

What input reading functions are there in C?

Expand Post »
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.
Reputation Points: 483
Solved Threads: 1
Posting Shark
EnderX is offline Offline
999 posts
since Aug 2006
May 29th, 2007
0

Re: What input reading functions are there in C?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 29th, 2007
0

Re: What input reading functions are there in C?

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...
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
May 29th, 2007
1

Re: What input reading functions are there in C?

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 30th, 2007
0

Re: What input reading functions are there in C?

Click to Expand / Collapse  Quote originally posted by Aia ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
May 30th, 2007
0

Re: What input reading functions are there in C?

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.
Attached Thumbnails
Click image for larger version

Name:	wrong c tagging.JPG
Views:	28
Size:	110.5 KB
ID:	3529  
Last edited by Aia; May 30th, 2007 at 6:09 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 30th, 2007
0

Re: What input reading functions are there in C?

I just tried it and it was ok -- If it persists for you please report it to Dani so that she can fix it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
May 30th, 2007
0

Re: What input reading functions are there in C?

There was a bug yesterday. It was fixed early this morning.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is online now Online
13,646 posts
since Feb 2002
May 31st, 2007
0

Re: What input reading functions are there in C?

Click to Expand / Collapse  Quote originally posted by Aia ...
  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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
May 31st, 2007
0

Re: What input reading functions are there in C?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: A Challenging Debugging Problem
Next Thread in C Forum Timeline: Helping with calling a function





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


Follow us on Twitter


© 2011 DaniWeb® LLC