Just in case:
you need
iostream
and
fstream
.
First, stop using
istream
and
ostream
. Use
ifstream
and
ofstream
, because iheritance is 1-way: if you're using member functions from
ofstream
, and you pass an
ostream
, the compiler will catch it as an error, because members of
ofstream
are not members of
ostream
like members of
ostream
are members of
ofstream
.
line 3, use the
bool good()
member function of ifstream. it returns true if no error flags are set.
on Line 7: instead of using
bool eof()
, use
bool good()
. Somthing might happen to the file that corrupts in in some way, and it could create an infinite loop (because although you're not at the enf od the file, you can't read from it, and if the stream becomes corrupted it will never reach the "end").
One last correction: you can use ifstream member function
peek()
to check the next character without removing it from the stream buffer. I suggest writing a function to check that there's a number before retrievint it from ifstream.
example:
1 2 3 4 5
|
/* You will need to write 'is_number()' for this*/
if(is_number(in.peek()) && in.good())
{
in>> a_number;
}
|
Lastly, I would break it down a little more. Write a function to look through the file, and a function to retrieve the data from a line.