How should i determine end of inputs
Moderator: Board moderators
-
- New poster
- Posts: 3
- Joined: Mon Mar 14, 2005 10:46 pm
How should i determine end of inputs
i dont know how should i determine endof inouts with standard inputoutput.
please help me
please help me
Suppose you have each input set in the following form:
integer1 integer2
(some imaginary problem that requires two input integers and you are supposed to read till the end of the file), then what you can do is:
Regards,
Suman.
integer1 integer2
(some imaginary problem that requires two input integers and you are supposed to read till the end of the file), then what you can do is:
Code: Select all
int main(void)
{
.....
/* loop - trying to read two integers from the stdin(standard input)
which is roughly - input from your keyboard, and check it
with what scanf returns i.e. the actual number of arguments it was
able to convert/read from stdin.On end of file, this will never equal
the desired number as scanf probably returns EOF.
Also, read the man page for scanf(), if you can access one, that is.
*/
while ( 2 == scanf("%d %d", &a, &b) )
{
/* do some processing here */
}
return 0;
}
Suman.
what sumankar means is something like
by the way, %s, %d, %f eliminate all preceeding white spaces so "%d %d" is redundant, though not wrong.
also, if you want to test eof for some reason, before the test, you should put in a scanf(" "), which consumes all white spaces in the input. This is a problem I've had and still see for the UVa problemset, because
won't get eof after last test input because there's probably another '\n' or some white space so you get WA for printing some extra line. you should either use scanf("%d%d ",...) or a scanf(" ") just before the end of the loop.
Hope this helps.
Code: Select all
for(;;){ //or other endless loop
if (scanf("%d%d",&int1,&int2)!=2) break;
// your processing code here
}
also, if you want to test eof for some reason, before the test, you should put in a scanf(" "), which consumes all white spaces in the input. This is a problem I've had and still see for the UVa problemset, because
Code: Select all
while( !feof(stdin) ){
scanf("%d%d",...);
//processing
}
Hope this helps.
Understanding a problem in a natural way will lead to a natural solution
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
oh, i see, u prefer to fgets because it has the second parameter so the buffer will not overflow. you're right, fgets is safer than gets.
but when i submit my code i use this:
#ifndef ONLINE_JUDGE
freopen("file.in", "r", stdin);
freopen("file.out", "w", stdout);
#endif
then i use the standard input and output operation, which one of them is gets.
but when i submit my code i use this:
#ifndef ONLINE_JUDGE
freopen("file.in", "r", stdin);
freopen("file.out", "w", stdout);
#endif
then i use the standard input and output operation, which one of them is gets.
If you know what this is doing you might as well have not submitted this when you are submitting solutions!The above is a compiler directive that makes the code fragment in between the #if /#endif stuff invisible during code generation i.e. roughly you can think of it as commenting out the parts you dont need.Ronald29 wrote: ...
but when i submit my code i use this:
#ifndef ONLINE_JUDGE
freopen("file.in", "r", stdin);
freopen("file.out", "w", stdout);
#endif
[snipped]
And as an alternative to this approach, I will tell you what I do:
I keep a file say input.txt, and I feed it to the program using the '<' operator which is equivalent to redirecting stdin, and get the output to some file using '>', which is equivalent to redirecting stdout, so there really is no need for fopen/freopen and friends.
Think of the situation where you don't have a matching fclose() to your open() - that is bad, even worse is when you have a fclose and its not #ifndef-ed out when submitting here!
And as Krzysztof said `gets is considered dangerous' - that 's the definition of gets().So forget about it.
Regards,
Suman.
that's exactly what i'm trying to do. I don't want the code to be run in the OJ because the file operation is prohibited, isn't it? you will get Restricted Function if you use the file operation function. (that's what i know). but i want to open from file and write to file to test the code in my computer, and i don't want to change my code. so i use the #ifndef ONLINE_JUDGE #endif since the OJ is define the ONLINE_JUDGE. in my computer, the freopen code will redirect the stdin and stdout to file that i want. you don't need to use fclose() function when using freopen(), since it will be closed on exit. and i'm only use this style of coding to submit code to the OJ.
best regards
ronald
best regards
ronald
So how are you checking for if freopen() succeeded at all or not ?
I gave you a workaround to your problem i.e. having input/output files & how
to use them *without* adding a single extra line in your code.You are of course, free to ignore any and all suggestions/advice.But I would suggest read well what I have to say and then come back.
Fooling around just beacuse some function says it will clean up the mess for you is *not* a good thing to do in general.
Regards,
Suman.
I gave you a workaround to your problem i.e. having input/output files & how
to use them *without* adding a single extra line in your code.You are of course, free to ignore any and all suggestions/advice.But I would suggest read well what I have to say and then come back.
Fooling around just beacuse some function says it will clean up the mess for you is *not* a good thing to do in general.
Regards,
Suman.