Page 1 of 1

just a Q

Posted: Fri Jan 07, 2005 1:02 pm
by ywceric
i wanna ask that whether i should use the file i/o or std i/o for the program??

Posted: Fri Jan 07, 2005 1:04 pm
by ..
std i/o

Posted: Fri Jan 07, 2005 1:31 pm
by ywceric
thx a lot~~
if then i wanna ask that how to know when is the end of input??

Posted: Fri Jan 07, 2005 2:56 pm
by misof
specify your programming language please :wink:

In C you may use feof(stdin)
in C++ cin.eof()
but be careful, whitespaces at the end of input may cause problems.

Usually the best way of EOF detection is when reading the next element fails.

E.g.:

while (scanf("%d",&N) == 1) { printf("read %d\n",N); }
while (cin >> N) { cout << "read " << N << endl; }

Posted: Fri Jan 07, 2005 3:49 pm
by ywceric
i am using C.
i've tried ""while (scanf("%d",&N) == 1) { printf("read %d\n",N); }"", but seems it doesn't work... even i input nothing and press enter, it doesn't stop.
how to use feof (sdtin)??
i only how to use feof(file)...

Posted: Fri Jan 07, 2005 5:23 pm
by Cahoun
ywceric wrote:i am using C.
i've tried ""while (scanf("%d",&N) == 1) { printf("read %d\n",N); }"", but seems it doesn't work... even i input nothing and press enter, it doesn't stop.
how to use feof (sdtin)??
i only how to use feof(file)...

Code: Select all

#include <stdio.h>

int N;

int main()
{
while (scanf("%d",&N)==1)
{
printf("read %d\n",N);
}
return 0;
}

Is it better? Tell us what did you forget please.

Code: Select all

if feof(stdin)
{
break;
}

Posted: Sat Jan 08, 2005 2:40 am
by misof
ywceric wrote:i am using C.
i've tried ""while (scanf("%d",&N) == 1) { printf("read %d\n",N); }"", but seems it doesn't work... even i input nothing and press enter, it doesn't stop.
how to use feof (sdtin)??
i only how to use feof(file)...
Cahoun: You probably misunderstood his current problem. The problem is: Even if you press Enter, the current input didn't end yet. The scanf function skips the whitespace you enter. Only after an explicit EOF (try typing Ctrl-D) the call of scanf() fails.

Maybe a more clear way is to redirect the input from a file, i.e. run
my_program < file.in
The stdio of your program will be replaced by the contents of file.in
Everything should behave exactly the way you are used to with files (except that you use scanf and printf instead of fscanf and fprintf).