how to avoid ifstream::get() when EOF is coming

D

drmario

I'm using VC++ 8.0 Express Edition on Windows XP Home Edition. I've
opened a file with ifstream::eek:pen(). The text in the file will have the
general format of:

michael jordan 90
roger moore 79
your name 100

So basically a first name, last name, grade, \n. I'm using
ifstream::get() to one by one read each char of the first and last name into
a cstring (no not string data type. An array of chars. I suppose
technically an array of pointers to chars). Then I use ifstream >> to read
the grade. It's kinda a long story why, but this is the (inefficient, I
know) way I've chosen to do it and I'm not going to change it.
Taking James Kanze's previous advice I inserted a buncha statements to
check the stream's error state flags after every input. I discovered that
after I read the last student's grade using ifstream >>, the program sets my
stream's eofbit to true. This of course makes sense. What does NOT make
sense to me is this after every iteration of reading a line, and echoing it
to the screen, my program reaches the getch() statement, and correctly
pauses until I hit a key then continues on. However, on its last iteration,
after it's read the very last thing in the text file and the eofbit is set
to true, I have to push like three or four keys.
Why is that happening? It makes 100% no sense to me. Why does the fact
that my ifstream's eofbit is set to true cause getch() to misbehave?
getch() has one job... wait until the next key is hit and return that value.
Why is this stupid ass ifstream::eofbit making it jack up? From my
understanding getch() doesn't even acknowledg the existence of a friggin
file stream, only the standard input stream.
Yes, as I'm sure you can tell, it pisses me off when Microsoft decides
not to include every detail of the inner workings of their STL
functionality. You'd think if there was anyone who could appreciate the
occasional need for subtle detials it would be the guys writing compilers.
Anyone I'm done venting. I hope someone can shed some light on why
getch() suddenly misbehaves after the eofbit is set to true. I'm stuck
fast, and have been so for weeks.

cheers,
Mario
 
P

Paul Carter

I'm using VC++ 8.0 Express Edition on Windows XP Home Edition. I've
opened a file with ifstream::eek:pen(). The text in the file will have the
general format of:

michael jordan 90
roger moore 79
your name 100

So basically a first name, last name, grade, \n. I'm using
ifstream::get() to one by one read each char of the first and last name into
a cstring (no not string data type. An array of chars. I suppose
technically an array of pointers to chars). Then I use ifstream >> to read
the grade. It's kinda a long story why, but this is the (inefficient, I
know) way I've chosen to do it and I'm not going to change it.
Taking James Kanze's previous advice I inserted a buncha statements to
check the stream's error state flags after every input. I discovered that
after I read the last student's grade using ifstream >>, the program sets my
stream's eofbit to true. This of course makes sense. What does NOT make
sense to me is this after every iteration of reading a line, and echoing it
to the screen, my program reaches the getch() statement, and correctly
pauses until I hit a key then continues on. However, on its last iteration,
after it's read the very last thing in the text file and the eofbit is set
to true, I have to push like three or four keys.
Why is that happening? It makes 100% no sense to me. Why does the fact
that my ifstream's eofbit is set to true cause getch() to misbehave?
getch() has one job... wait until the next key is hit and return that value.
Why is this stupid ass ifstream::eofbit making it jack up? From my
understanding getch() doesn't even acknowledg the existence of a friggin
file stream, only the standard input stream.
Yes, as I'm sure you can tell, it pisses me off when Microsoft decides
not to include every detail of the inner workings of their STL
functionality. You'd think if there was anyone who could appreciate the
occasional need for subtle detials it would be the guys writing compilers.
Anyone I'm done venting. I hope someone can shed some light on why
getch() suddenly misbehaves after the eofbit is set to true. I'm stuck
fast, and have been so for weeks.

cheers,
Mario

Why don't you post your code? It almost has to be something you are
doing wrong, but it's hard to tell from just your description of the
program and not the program itself.

I think it's more likely that your loop is continuing after the eofbit
is set to true than getch() is breaking, but it's impossible to check
without seeing your code.

Thanks,

Paul
 
J

Jerry Coffin

I'm using VC++ 8.0 Express Edition on Windows XP Home Edition. I've
opened a file with ifstream::eek:pen(). The text in the file will have the
general format of:

michael jordan 90
roger moore 79
your name 100

So basically a first name, last name, grade, \n. I'm using
ifstream::get() to one by one read each char of the first and last name into
a cstring (no not string data type. An array of chars. I suppose
technically an array of pointers to chars). Then I use ifstream >> to read
the grade. It's kinda a long story why, but this is the (inefficient, I
know) way I've chosen to do it and I'm not going to change it.

So your question is: "How do I do this correctly while insisting on
doing it completely incorrectly?"

If you're not using a string, you probably want to use an array of char,
but almost certainly NOT an array of pointers to char. You're starting
with a poor idea, but that would be far worse still.
Taking James Kanze's previous advice I inserted a buncha statements to
check the stream's error state flags after every input. I discovered that
after I read the last student's grade using ifstream >>, the program sets my
stream's eofbit to true. This of course makes sense.

No, it does NOT! After reading the last data in the file, eofbit should
NOT be set yet. You should see eofbit set ONLY when you try to read
something and the final data in the file had _previously_ been read.

If you insist on reading the names into arrays of char (why?) you could
take a look at ifstream::getline to do the job. Since you're reading
structured data, I'd advise reading the data into a structure:

struct person {
const int len = 20;
char first_name[len];
char last_name[len];
int grade;
};

std::ifstream &operator>>(std::ifstream &in, person &p) {
in.getline(first_name, len, ' ');
in.getline(last_name, len, ' ');
in >> grade;
return in;
}

Then reading a file full of data in that format into a vector (for
example) looks something like:

std::vector<person> people;

std::copy(std::istream_iterator<person>(input),
std::istream_iterator<person>(),
std::back_inserter(people));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top