Code:
int main ( ) {
char buff[BUFSIZ];
while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
fputs( buff, stdout );
}
return 0;
}
If you run this program from the command line as
prog.exe
then you would just type stuff in.
Or you could run it like this, to get input from a file
prog.exe < input.txt
Code:
int main ( int argc, char *argv[] ) {
char buff[BUFSIZ];
FILE *fp = fopen( argv[1], "r" );
while ( fgets( buff, sizeof buff, fp ) != NULL ) {
fputs( buff, stdout );
}
return 0;
}
You could run this program as
prog.exe input.txt
By changing just a few lines, you can flexibly read input from either stdin or from a file. Once the line of input is in 'buff', you can do whatever you want to it, with whatever functions you like.
One advantage of sscanf() over scanf/fscanf is that it doesn't alter the state, so you can have several passes over the same buffer with sscanf().
A good example is getting the user to type in a date in a flexible format (12/13/06 or Dec-13), and figuring out which format they chose.