Help with simple file reading issue please

S

Shane

Hi,

Thanks in advance for the help. I have been to many websites and tried
several solutions to my problem, but have fixed part of it. It's time to
come humbly to the newsgroups for help :)

By the way, please don't reply just to tell me that my code isn't optimized
and that I should use a delimited database rather than a line by line
record. If I had my way, I'd do it differently too.

I am reading in a file that looks like this..
<snip>
 
S

Shane

Thanks in advance for the help. I have been to many websites and tried
several solutions to my problem, but have fixed part of it. It's time to
come humbly to the newsgroups for help :)

By the way, please don't reply just to tell me that my code isn't optimized
and that I should use a delimited database rather than a line by line
record. If I had my way, I'd do it differently too.

I am reading in a file that looks like this..
<snip>
Crap, let's try that again..

uh, looks like this..

item2
type
dude
(e-mail address removed)
4.44
avail

With code that looks like this...

Multi is an ifstream

Multi.open("Multimedia.txt");
if (!Multi){}
else{
while (!Multi.eof()){
m = new Multimedia();
string Item;
getline(Multi, Item, '\n');
m->setName(Item);
string Type;
getline(Multi, Type, '\n');
m->setType(Type);
string By;
getline(Multi, By, '\n');
m->setAuthor(By);
string Owner;
getline(Multi, Owner, '\n');
m->setOwner(Owner);
float Price;
Multi >> Price;
Multi.ignore(100,'\n');
m->setPrice(Price);
string Availability;
getline(Multi, Availability, '\n');
m->setStatus(Availability);

MM.push_back(*m); //linked list (STL)
}
}
Multi.close();

It was worse until I added the Multi.ignore line, but I still have a
problem.
My result is two records instead of one. Obviously I am pulling the price
information out twice (left in buffer?) and it is obvious that it has to do
with the fact that I am using cin to get it. I can't use getline for
getting a float, so I need to find a way to clear the buffer out.

Am I even on the right track? Can someone help me please?

Shane
 
D

David Hilsee

Shane said:
Crap, let's try that again..

uh, looks like this..

item2
type
dude
(e-mail address removed)
4.44
avail

With code that looks like this...

Multi is an ifstream

Multi.open("Multimedia.txt");
if (!Multi){}
else{
while (!Multi.eof()){
m = new Multimedia();
string Item;
getline(Multi, Item, '\n');
m->setName(Item);
string Type;
getline(Multi, Type, '\n');
m->setType(Type);
string By;
getline(Multi, By, '\n');
m->setAuthor(By);
string Owner;
getline(Multi, Owner, '\n');
m->setOwner(Owner);
float Price;
Multi >> Price;
Multi.ignore(100,'\n');
m->setPrice(Price);
string Availability;
getline(Multi, Availability, '\n');
m->setStatus(Availability);

MM.push_back(*m); //linked list (STL)
}
}
Multi.close();

It was worse until I added the Multi.ignore line, but I still have a
problem.
My result is two records instead of one. Obviously I am pulling the price
information out twice (left in buffer?) and it is obvious that it has to do
with the fact that I am using cin to get it. I can't use getline for
getting a float, so I need to find a way to clear the buffer out.

Am I even on the right track? Can someone help me please?

That looks like the old "improper use of eof" problem. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 15 ("Input/output via
<iostream> and <cstdio>"), question 5 ("Why does my input seem to process
past the end of file?").

By the way, the dynamic allocation of the Multimedia object (m = new
Multimedia();) looks like it is unnecessary and possibly causing a memory
leak. Did you mean to write "Multimedia m;"?
 
S

Shane

That looks like the old "improper use of eof" problem. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 15 ("Input/output via
<iostream> and <cstdio>"), question 5 ("Why does my input seem to process
past the end of file?").

By the way, the dynamic allocation of the Multimedia object (m = new
Multimedia();) looks like it is unnecessary and possibly causing a memory
leak. Did you mean to write "Multimedia m;"?

I looked at that exact article before, but ignored it because it skips the
whole purpose of using getline for everything else. In other words, if my
record looks like this:

Item name with spaces
Record
Full Name with spaces
(e-mail address removed)
4.44
Available on request with spaces

then I have issues. So I tried your suggestion, but I lose the first cin in
the record to the x variable. This throws the whole record off. If I use
getline, I can grab everything in the line and pull it into the variable of
my choice. Am I missing something?

Thanks again for the help!
Shane
 
D

David Hilsee

Shane said:
I looked at that exact article before, but ignored it because it skips the
whole purpose of using getline for everything else. In other words, if my
record looks like this:

Item name with spaces
Record
Full Name with spaces
(e-mail address removed)
4.44
Available on request with spaces

then I have issues. So I tried your suggestion, but I lose the first cin in
the record to the x variable. This throws the whole record off. If I use
getline, I can grab everything in the line and pull it into the variable of
my choice. Am I missing something?

Thanks again for the help!

Yes, the example uses operator>>, but the idea can be applied to many other
circumstances as well. The problem is the same: eof() is being set after
you attempt to read past the end of the file. The first iteration does not
attempt to read _past_ the end of the file, so the loop continues on for
another iteration, adding a second object to the linked list. You do not
check to see if any of the input operations succeed, so the second iteration
silently fails to retrieve input from the file after the first iteration
read all of the input.

Now, suppose there were a function std::istream& operator>>(std::istream&,
Multimedia&) that would allow you to read a Multimedia from a stream. Then
you could write your loop as

Multimedia m;
while( Multi >> m ) {
MM.push_back(m);
}

if ( !Multi.eof() ) {
// Something bad happened. Should have attempted to read past
// the end of the file to end the loop.
}

The above would silently ignore a partial Multimedia entry, but you could
fix that, if you like. The other option would be to add error handling to
your code. For example,

getline(Multi, Item, '\n');

should be changed so you know if the read was successful, like so

if ( !getline(Multi, Item, '\n') ) {
std::cerr << "Error reading Item" << std::endl;
break;
}

You'll need to add similar error handling for the other input operations you
perform. Your code will now complain loudly when it fails to read from the
file. However, that would still render your call to eof() useless, because
the loop will most likely end once an error has occurred, not when eof()
returns true. Give those changes a shot and see if it helps you understand
what's going on.
 
J

Jack Klein

I looked at that exact article before, but ignored it because it skips the
whole purpose of using getline for everything else. In other words, if my
record looks like this:

Item name with spaces
Record
Full Name with spaces
(e-mail address removed)
4.44
Available on request with spaces

then I have issues. So I tried your suggestion, but I lose the first cin in
the record to the x variable. This throws the whole record off. If I use
getline, I can grab everything in the line and pull it into the variable of
my choice. Am I missing something?

Thanks again for the help!
Shane

Yes, you are missing the fact that in C++ and C, from which C++
inherits the behavior, end of file does not work the way you think.
Not for C style <stdio.h> streams nor for C++ stream objects.

The end of file indicator is NEVER set for a stream because you just
read the last item from the stream and the NEXT read will fail. The
end of file indicator is only set for a stream AFTER you try to read
PAST THE END of the file.

You are checking for end of file before you do the read. If there is
one item in a file and you have read that one item, the end of file
indicator will be false. When you try to read the second item, there
is nothing left in the file and the read fails, sets the end of file
indicator, and leaves the destination untouched. So it still contains
whatever was previously in it, perhaps the first record.

You need to do the check for end of file either after reading, or
while reading the data.
 
D

Daniel T.

Thanks in advance for the help. I have been to many websites and tried
several solutions to my problem, but have fixed part of it. It's time to
come humbly to the newsgroups for help :)

By the way, please don't reply just to tell me that my code isn't optimized
and that I should use a delimited database rather than a line by line
record. If I had my way, I'd do it differently too.

I am reading in a file that looks like this..
<snip>
Crap, let's try that again..

uh, looks like this..

item2
type
dude
(e-mail address removed)
4.44
avail

With code that looks like this...

Multi is an ifstream

Multi.open("Multimedia.txt");
if (!Multi){}
else{
while (!Multi.eof()){
m = new Multimedia();
string Item;
getline(Multi, Item, '\n');
m->setName(Item);
string Type;
getline(Multi, Type, '\n');
m->setType(Type);
string By;
getline(Multi, By, '\n');
m->setAuthor(By);
string Owner;
getline(Multi, Owner, '\n');
m->setOwner(Owner);
float Price;
Multi >> Price;
Multi.ignore(100,'\n');
m->setPrice(Price);
string Availability;
getline(Multi, Availability, '\n');
m->setStatus(Availability);

MM.push_back(*m); //linked list (STL)
}
}
Multi.close();

It was worse until I added the Multi.ignore line, but I still have a
problem.
My result is two records instead of one. Obviously I am pulling the price
information out twice (left in buffer?) and it is obvious that it has to do
with the fact that I am using cin to get it. I can't use getline for
getting a float, so I need to find a way to clear the buffer out.

Am I even on the right track? Can someone help me please?[/QUOTE]

Try this:

istream& operator>>( istream& s, Multimedia& m )
{
string item;
getline( s, item );
m.setName( item );
getline( s, item );
m.setType( item );
// etc...
return s;
}

Then in your code:

Multimedia m;
while ( Multi >> m )
MM.push_back( m );
 
S

Shane

Yes, the example uses operator>>, but the idea can be applied to many other
circumstances as well. The problem is the same: eof() is being set after
you attempt to read past the end of the file. The first iteration does not
attempt to read _past_ the end of the file, so the loop continues on for
another iteration, adding a second object to the linked list. You do not
check to see if any of the input operations succeed, so the second iteration
silently fails to retrieve input from the file after the first iteration
read all of the input.

Now, suppose there were a function std::istream& operator>>(std::istream&,
Multimedia&) that would allow you to read a Multimedia from a stream. Then
you could write your loop as

Multimedia m;
while( Multi >> m ) {
MM.push_back(m);
}

if ( !Multi.eof() ) {
// Something bad happened. Should have attempted to read past
// the end of the file to end the loop.
}

The above would silently ignore a partial Multimedia entry, but you could
fix that, if you like. The other option would be to add error handling to
your code. For example,

getline(Multi, Item, '\n');

should be changed so you know if the read was successful, like so

if ( !getline(Multi, Item, '\n') ) {
std::cerr << "Error reading Item" << std::endl;
break;
}

You'll need to add similar error handling for the other input operations you
perform. Your code will now complain loudly when it fails to read from the
file. However, that would still render your call to eof() useless, because
the loop will most likely end once an error has occurred, not when eof()
returns true. Give those changes a shot and see if it helps you understand
what's going on.

Thanks for all the suggestions. It's working properly now :)

Shane
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top