Page 1 of 2
How should i determine end of inputs
Posted: Wed Mar 16, 2005 7:54 pm
by hamidreza_buddy
i dont know how should i determine endof inouts with standard inputoutput.
please help me
Posted: Thu Mar 17, 2005 6:25 am
by sumankar
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:
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;
}
Regards,
Suman.
Posted: Thu Apr 28, 2005 9:08 am
by jakabjr
what sumankar means is something like
Code: Select all
for(;;){ //or other endless loop
if (scanf("%d%d",&int1,&int2)!=2) break;
// your processing code here
}
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
Code: Select all
while( !feof(stdin) ){
scanf("%d%d",...);
//processing
}
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.
Posted: Sat May 14, 2005 9:07 pm
by N|N0
i would like to ask you people what's the shortest way in c of parsing an input with unknown number of line with unknown number of ints in each (also different in each) where you also got to have the information in which line the number was.

Posted: Sun May 15, 2005 5:37 am
by chunyi81
Try with the gets function. Then use strtok function in string.h library to extract the integers I am not sure if there is any shorter way. Hope this helps.
Posted: Sun May 15, 2005 2:55 pm
by Krzysztof Duleba
gets is considered dangerous. fgets should be used instead.
Posted: Mon May 16, 2005 8:11 am
by Ronald29
gets is considered dangerous. fgets should be used instead. <-
if you use freopen, then the gets function will do the same as fgets
Posted: Mon May 16, 2005 1:15 pm
by sumankar
You are not supposed to do file handling here, you
*) read from stdin
*) write to stdout
which are managed by native OS for you unless otherwise stated.So freopen is not good advice in this nick of the woods
Regards,
Suman.
Posted: Mon May 16, 2005 3:24 pm
by Krzysztof Duleba
Ronald29 wrote:if you use freopen, then the gets function will do the same as fgets
That's very not true. freopen has nothing to do here. gets is dangerous because it's susceptible to buffer overflows. That's not the main issue in programming contests, but one should avoid bad habits.
Posted: Tue May 17, 2005 5:32 am
by Ronald29
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.
Posted: Tue May 17, 2005 6:09 am
by sumankar
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]
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.
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.
Posted: Wed May 18, 2005 6:07 am
by Ronald29
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
Posted: Wed May 18, 2005 8:12 am
by sumankar
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.
Posted: Wed May 18, 2005 10:02 am
by Ronald29
sorry, my mistake...

Posted: Wed Jun 01, 2005 3:11 pm
by jakabjr
does the < and > redirect work under windows or just linux systems?
srry for asking instead of testing, but it would be harder to test, since i don't use windows.