EOF

Thread Starter

afaik

Joined Nov 2, 2008
22
I'm using dev-c++ and when I input characters into the code below, for example 'asdf', it loops fine. If I enter 'asdf(ctrl-z)' for end of file, program will output the asdf, but wont display cout << "eof reached" or exit the loop. If I enter ctrl-z by itself, program will output cout << "eof reached" and exit the loop. Can anyone point out what I'm doing wrong?

-I probably could do this better with a switch, that would be tomorrows exercise

Rich (BB code):
#include <iostream>

using namespace std;

int main()
{

char temp;
char nwln = '\n';

cout << "EOF: " << EOF << endl;
cout << "Input something: ";

//while(nwln != temp)
do
{
  temp = cin.get();
//  cin >> temp;

  if(temp != '\n' && temp != EOF)  //cin.eof() )
    cout << "You inputted:" << temp << "." << endl;

  if(temp == '\n' )
    cout << "New Line entered" << endl;

  if(cin.eof() ) // if(temp == -1)  // if(temp == EOF) 
    cout << "END OF FILE REACHED";   

}while(!cin.eof() );  

cout << "\n\n" ; 
system("PAUSE");
}
 

RiJoRI

Joined Aug 15, 2007
536
While I don't know C++, I'd suggest to begin by simplifying the loop:
Rich (BB code):
do{
}while(!cin.eof() );
cout << "Got EOF\n" ;
... and make sure it works. Then add the tests singly -- one and only one test in the loop at a time. I'd even split the multiple if statement. This way you can find out which statement is giving you trouble. If they all pass on their own, then there may be an ordering problem.

My guess is that "temp = cin.get();" is eating the EOF character, however, is IS a guess.

--Rich


--Rich
 

Mark44

Joined Nov 26, 2007
628
Rich's advice is good. I would also add that the two calls to cin.eof() in each loop iteration might be causing problems.

You said that you are using a dev-C++ compiler. I've never heard of it, but if you have any documentation on the behavior of the eof() function, it might be helpful in understanding what's going on here. Each compiler vendor implements the run-time classes in its own way, so without having that compiler I can't say what the behavior is. In any case, my guess is that after you have called eof() the first time, you shouldn't be calling eof() a second time if there has been no change in the stream you're reading from.

If your compiler comes with a debugger, and you know how to use it, that would be extremely useful. You could use it to single-step through the code to see what happens to the EOF character (CTRL-Z) in the two cases you tested.
 

Thread Starter

afaik

Joined Nov 2, 2008
22
Something definitely funny is happening at "temp = cin.get();" After playing around a bit with the debugger (that really is useful), I need to do reading and see how cin.get() will handle an EOF value. Because I'm saving to a char and an EOF, according to my somewhat outdated book, says it "returns non-zero (true) if eof has been encountered in designated stream". I'll try again later tonight for a bit, hopefully I can make progress with it and continue learning instead of being stuck on the same spot. I'll post my solution if I find it.
 

Thread Starter

afaik

Joined Nov 2, 2008
22
Unbelievable...After playing around with this for two weeks and learning NOTHING, I've found some info that might help anyone else having problems with EOF on a windows machine.

Bjarne Stroustrup:
The program ends reading input when it sees "end of file". If you run the program from the keybord on a Unix machine "end of file" is Ctrl-D. If you are on a Windows machine that because of a bug doesn't recognize an end-of-file character...
http://www.research.att.com/~bs/bs_faq2.html#simple-program
(Scroll to last bullet of that part)

Google Groups:
http://groups.google.com/group/comp...e=utf-8&q=eof+windows+stream#da6fb742a91082cc
"Hello!
I have a problem with a C++ program under Windows but which works fine
under Linux. The program simply reads in integers from a file (source
code below)...
In Linux it reads all the numbers in the file no matter how EOF is
positioned: right after the last number or on the next line in
in_file. In Windows it won't read the last number unless there's an
empty line at the end of the file.
Example:
1 2 3 4 5 6 7EOF -> this works under Linux but not under Windows
1 2 3 4 5 6 7<CR>
EOF -> this works under both OS's "
 
Top