| | |
What input reading functions are there in C?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2006
Posts: 999
Reputation:
Solved Threads: 1
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.
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.
Kumquat.
>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:
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.
>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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> int getint ( void ) { char buff[BUFSIZ]; int result = 0; int ch; /* Pull all digits and add them to the result value */ while ( ( ch = getchar() ) != EOF && isdigit ( ch ) ) { result = 10 * result + ( ch - '0' ); } /* Return the unconverted character to the stream */ ungetc ( ch, stdin ); return result; }
Last edited by Narue; May 29th, 2007 at 12:14 pm.
I'm here to prove you wrong.
And don't try to shortcut the
Here's why. Read the entire
fgets()/sscanf() pair with scanf(). If someone suggests it, stare at him in horror and cast the daemon away! 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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
A little example of using what it has been said so far.
Sorry, the proper c tagging is not working at this time.
Sorry, the proper c tagging is not working at this time.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> int getinteger( int *result ) { int send = 0; char ch; char buffer [ 13 ] = { '\0' }; if( fgets( buffer, sizeof buffer, stdin ) && !isspace( *buffer ) && sscanf( buffer, "%d%c", result, &ch ) == 2 && ( ch == '\n' || ch == '\0' ) ) { send = 1; } else { if( buffer[ strlen( buffer ) - 1 ] != '\n' ) /* if newline is not found in string */ { while( getchar() != '\n' ); /* clear stdin */ } send = 0; } return send; } int main( void ) { int number = 0; do { fputs( "Enter an integer: ", stdout ); fflush( stdout ); } while ( !getinteger( &number ) ); printf( "number = %d\n", number ); getchar(); return 0; }
Last edited by Aia; May 29th, 2007 at 8:45 pm.
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.
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
•
•
C Syntax (Toggle Plain Text)
if( fgets( buffer, sizeof buffer, stdin ) && !isspace( *buffer ) && sscanf( buffer, "%d%c", result, &ch ) == 2 && ( ch == '\n' || ch == '\0' ) )
Could you try to clean it up?
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
>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.
>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.
![]() |
Other Threads in the C Forum
- Previous Thread: A Challenging Debugging Problem
- Next Thread: Helping with calling a function
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






