just a Q

Post here if you don't find any other place for your post. But please, stay on-topic: algorithms, programming or something related to this web site and its services.

Moderator: Board moderators

Post Reply
ywceric
New poster
Posts: 5
Joined: Fri Jan 07, 2005 12:58 pm

just a Q

Post by ywceric »

i wanna ask that whether i should use the file i/o or std i/o for the program??
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

std i/o
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.
ywceric
New poster
Posts: 5
Joined: Fri Jan 07, 2005 12:58 pm

Post by ywceric »

thx a lot~~
if then i wanna ask that how to know when is the end of input??
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post 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; }
ywceric
New poster
Posts: 5
Joined: Fri Jan 07, 2005 12:58 pm

Post 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)...
Cahoun
New poster
Posts: 13
Joined: Mon Jan 03, 2005 2:34 pm
Location: Czech Republic
Contact:

Post 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;
}
Learn, learn, learn.
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post 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).
Post Reply

Return to “Other words”