How should i determine end of inputs

Write here if you have problems with your C source code

Moderator: Board moderators

hamidreza_buddy
New poster
Posts: 3
Joined: Mon Mar 14, 2005 10:46 pm

How should i determine end of inputs

Post by hamidreza_buddy »

i dont know how should i determine endof inouts with standard inputoutput.
please help me
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post 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.
jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post 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.
Understanding a problem in a natural way will lead to a natural solution
N|N0
New poster
Posts: 36
Joined: Tue Jan 25, 2005 10:33 pm
Location: Ljubljana, Slovenija
Contact:

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

:)
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post 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.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

gets is considered dangerous. fgets should be used instead.
Ronald29
New poster
Posts: 15
Joined: Sat May 24, 2003 3:57 am

Post 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
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post 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 8)

Regards,
Suman.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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.
Ronald29
New poster
Posts: 15
Joined: Sat May 24, 2003 3:57 am

Post 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.
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post 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.
Ronald29
New poster
Posts: 15
Joined: Sat May 24, 2003 3:57 am

Post 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
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post 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.
Ronald29
New poster
Posts: 15
Joined: Sat May 24, 2003 3:57 am

Post by Ronald29 »

sorry, my mistake...
:D
jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post 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.
Understanding a problem in a natural way will lead to a natural solution
Post Reply

Return to “C”