Reputation: 53
I'm trying to redirect a file as stdin input (one of the requirements). I can't figure out how to check if the next input is empty or if it's done.
something like this
./a.out program < file.txt
So here is what I am trying to do.
char string[10];
while ( the input is NOT empty)
{
scanf("%s",&string);
printf("%s",string);
}
The given file looks something like this
abc
abcd
abcde
abcdef
Upvotes: 2
Views: 2383
Reputation: 726499
You can call feof
on stdin
:
while (!feof(stdin)) {
scanf("%s", string); // You do not need & for strings
printf("%s",string);
}
Upvotes: 2
Reputation: 375
If you do the following, it will only stop when scanf
cannot read anything else.
while( scanf("%s", string) != EOF ){
printf("%s", string);
}
By the way to scan a string we cannot make use of &
as it already is a pointer.
Upvotes: 2