What I'm doing is very basic, but it's to familiarize myself with basic input/output. I'm using dev-c++ and when I input characters into the program 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 or where my logic is wrong.
#include <iostream>
using namespace std;
int main() {
char temp;
const char NEWLN = '\n'; //style correction, also since newln isn't ever assigned to, make it const
cout << "EOF: " << EOF << endl;
cout << "Input something: ";
do
{
cin.get(temp);
//without the condition change here, with an input of 'asdf^Z\n', eof won't be "found" for your main loop,
//even though *this* loop will exit, because the prev version of this loop ignores eof and will advance cin past it
while(temp != NEWLN && !cin.eof())
{
if(temp != NEWLN && temp != cin.eof() )
cout.put(temp) ;
cin.get(temp);
}
if(temp == NEWLN )
//cout << "New Line entered" << endl;
cout << "/n" << endl;
if(cin.eof() )
cout << "END OF FILE REACHED";
cout << "\nEOF: " << cin.eof() << endl;
}while(!cin.eof() );
cout << "\n\n" ;
system("PAUSE");
}
#include <iostream>
using namespace std;
int main() {
char temp;
const char NEWLN = '\n'; //style correction, also since newln isn't ever assigned to, make it const
cout << "EOF: " << EOF << endl;
cout << "Input something: ";
cin.get(temp) //priming read
while (cin) //this detects EOF as well as stream read errors!
{
if(temp == NEWLN )
cout << endl; //endl prints a newline, as well as forcing the output to be written to screen. unless you really wanted to doublespace?
else if(cin.eof() )
cout << "END OF FILE REACHED";
else //must be a char we want to put back
cin.put(temp);
cout << "\nEOF: " << cin.eof() << endl;
cin.get(temp);
}
cout << "\n\n" ;
system("PAUSE");
return 0; //always put a return value on main so you're in control of what you return to OS on success
}
Last edited by MarauderIIC on Thu Jan 15, 2009 8:31 am, edited 1 time in total.
Reason:see sig
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Well, I spent some time with it tonight but haven't made much progress. The only realization is that once the program gets to ctrl-z after going through lets say 'asdf(ctrl-z)', it doesn't accept ctrl-z, as input.
If I do ctrl-z as the only input, it will bypass while(cin) altogether.
So unless I'm wrong, ctrl-z (EOF) isn't accepted by cin, . Sigh, I've been working on this for a couple days on and off trying to solve this alone and haven't made progress. I'll see if I can ask around, but if anyone here knows how to handle this off the top of their heads, it would save me a couple more hours.
EOF is reached when EOF is reached. I wasn't sure about cin taking EOF either, myself (since there's not necessarily any END to input). File I/O you use essentially the structure I gave you (priming read, while (file)).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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.pro ... 42a91082cc
"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 "
I see. Thanks for posting your solution :) Sorry it gave you so much trouble (and we weren't much help). TBH I've never needed it to read to an eof character, I just read until there's nothing else to read in the file, as the loop I posted works just fine. For user input, I've simply used '\n' as a delimiter for getline() and ' ' as the delimiter for cin, in other words, the defaults.
Also, I'm not sure that cin.eof() detects if input failed in the way I would think of it (unlike what his comment says) -- such as inputting a char for an int. "!cin" would, though. It's shorthand for either !cin.good() or cin.bad(), I forget which.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Even though I stumbled across what I think was the reason for my problems, I took your code and played with it for a while. Thanks for your help and hopefully one day, sooner than later, I can help others with their own problems.