![]() |
| | #1 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Simple EOF question I am currently studying K&R and I was looking at the example as follows: Code: #include <stdio.h>
main ()
{
int c;
c = getchar();
while (c != EOF)
{
putchar(c);
c = getchar();
}
}
|
| deadhippo is offline | |
| | #2 |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| http://faq.cprogramming.com/cgi-bin/...&id=1043284351 Likely your program expects redirected input: Code: c:\myprog.exe < file.txt
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #3 |
| Registered User Join Date: Apr 2008
Posts: 31
| If you want to read until the end of input, when you are reading from stdin (i.e. if the user is typing). You want to look for end of line, or from a string (char-array) a NULL-value (0). If you type "-1", it will read the character - and 1, which have ascii-values which are not -1 either together or in their parts. Instead you could look for 10 or '\n' (that is if you expect typed input): Code: while (c != '\n')
{
putchar(c);
c = getchar();
}
|
| Zarniwoop is offline | |
| | #4 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks for the replies. I am still a little confused though. I thought because EOF is defined in stdio.h as -1, that when I type in -1 the program should end. Your suggestions work of course and I think understand you explanation about ascii values but I wonder why nearly every example in Kernighan and Ritchie uses EOF if it doesn't work. |
| deadhippo is offline | |
| | #5 | |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| Quote:
EOF is not what was read from input stream - it is artifitially defined value that cannot be in any case read from input stream, and thus was selected to notify the caller that there is nothing left to read... to imitate the EOF condition from the keyboard you can try to use Ctrl-Z combination on Windows or Ctrl-D on *nix system...
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. | |
| vart is offline | |
| | #6 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks for the reply. I'm not really sure about the reasoning but I guess I'll try to just accept it as a fact. Why is EOF defined as -1 in stdio.h though? Ctrl-C worked for me. Thanks for the suggestion. |
| deadhippo is offline | |
| | #7 | |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| Quote:
closes impossible values are -1 and 256 because errors in general are notified with negative values - I think -1 was choosen to indicate the read error
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. | |
| vart is offline | |
| | #8 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks again. |
| deadhippo is offline | |
| | #9 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > Ctrl-C worked for me. Thanks for the suggestion. No, that just kills the program. It is not an orderly exit. > Why is EOF defined as -1 in stdio.h though? All chars are small positive integers, so it kinda made sense (I guess) to pick a value which could be never equal to a char no matter how large a char value got. ctrl-d or ctrl-z will work, but exactly how they work depend on your terminal driver and the I/O code in the C runtime library provided by your compiler. Normally you press the control key sequence at the start of a line, and sometimes you have to press return afterwards (if the input is buffered). If you try to press the control sequence at any other time, then some systems need you to press the same sequence again (eg. ctrl-z ctrl-z). Expermiment with this in a simple program to see how your specific implementation behaves. Code: if ( ch == EOF ) {
printf( "yes, that was EOF\n" );
}
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #10 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks for the reply and the clarification. I'm not quite sure what to do with that program yu gave me. Should I use it combination with the previous program or as a standalone. I tried it as a standalone but was told i had to declare c so I created the following program but nothing happens. Code: #include <stdio.h>
main()
{
int ch;
if ( ch == EOF )
{
printf( "yes, that was EOF\n" );
}
}
|
| deadhippo is offline | |
| | #11 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Put it in a loop which prints say the decimal, hex and character value of every character received.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #12 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,364
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #13 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| It could be any integer constant not equal to any valid character I suppose. My guess is that it started off as a literal -1, then came the EOF symbol which typically was assigned the value -1 just to avoid any surprises with any old code using a magic number.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Tags |
| eof |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question about 'gets' and EOF | Sharke | C Programming | 4 | 03-11-2009 08:41 AM |
| Simple class question | 99atlantic | C++ Programming | 6 | 04-20-2005 11:41 PM |
| Simple question about pausing program | Noid | C Programming | 14 | 04-02-2005 09:46 AM |
| simple question. | InvariantLoop | Windows Programming | 4 | 01-31-2005 12:15 PM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |