filestream problem

P

Pieter Provoost

I'm having a little filestream problem. With the code below, I open a file
in order to insert its contents into another file. However, right after the
opening of the file, insertfile.eof() seems to be TRUE. Before the
problematic file is opened, one other file is opened and inserted by the
same piece of code (which is in a loop), without any problems. I'm sure that
the file to be inserted has several lines of contents, and I also checked if
the insertfile stream is closed before opening it.

if(FileExists(insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(! insertfile.eof())
{
getline(insertfile, insertline);
outfile << insertline << endl;
}
insertfile.close();
}

I would really appreciate some advise here.

Thanks,
Pieter
 
V

Victor Bazarov

Pieter said:
I'm having a little filestream problem. With the code below, I open a file
in order to insert its contents into another file. However, right after the
opening of the file, insertfile.eof() seems to be TRUE. Before the
problematic file is opened, one other file is opened and inserted by the
same piece of code (which is in a loop), without any problems. I'm sure that
the file to be inserted has several lines of contents, and I also checked if
the insertfile stream is closed before opening it.

if(FileExists(insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(! insertfile.eof())
{
getline(insertfile, insertline);
outfile << insertline << endl;
}
insertfile.close();

Add

insertfile.clear();

here. The 'eof' bit gets carried over and is not cleared by the 'open'
operation, most likely.
}

I would really appreciate some advise here.

HTH

V
 
P

Pieter Provoost

Victor Bazarov said:
Add

insertfile.clear();

here. The 'eof' bit gets carried over and is not cleared by the 'open'
operation, most likely.


Works fine, thanks! I forgot to mention that I'm using BCB6, I don't know if
this problem also occurs with other compilers...

Pieter
 
K

Karl Heinz Buchegger

Pieter said:
I'm having a little filestream problem. With the code below, I open a file
in order to insert its contents into another file. However, right after the
opening of the file, insertfile.eof() seems to be TRUE. Before the
problematic file is opened, one other file is opened and inserted by the
same piece of code (which is in a loop), without any problems. I'm sure that
the file to be inserted has several lines of contents, and I also checked if
the insertfile stream is closed before opening it.

if(FileExists(insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(! insertfile.eof())
{
getline(insertfile, insertline);
outfile << insertline << endl;
}
insertfile.close();
}

I would really appreciate some advise here.

you are using eof() in the wrong way
(I am just waiting for your next posting entitled:
"why does the last line of my file get processed twice?")
 
V

Victor Bazarov

Pieter said:
"Victor Bazarov" <[email protected]> schreef in bericht
[..] The 'eof' bit gets carried over and is not cleared by the 'open'
operation, most likely.



Works fine, thanks! I forgot to mention that I'm using BCB6, I don't know if
this problem also occurs with other compilers...

I have no idea about BCB6. AFAICT, it's standard behaviour.

V
 
P

Pieter Provoost

Karl Heinz Buchegger said:
you are using eof() in the wrong way
(I am just waiting for your next posting entitled:
"why does the last line of my file get processed twice?")


"I would really appreciate some advise here."
The last line is not processed twice, btw.

Pieter
 
K

Karl Heinz Buchegger

Pieter said:
"I would really appreciate some advise here."

See below
The last line is not processed twice, btw.

It is. You just haven't noticed it :)
Compare the input file closely with the output file and you will
see that the last line is duplicated. In case the last line is an empty
line, you will find, that your output file has one empty line more then
the input file. It is easy to miss, but it is there. If you don't
believe me, make an experiment: In your input file, in the last line,
write some text but don't terminate that line with a 'return'. Then
you will see.

The point is:
eof() turns to true only after you tried *and failed* to read past
the end of file. C++ has no crystal ball, like other languages. It doesn't
try to predict the future.

eof() is ment to be used to figure out why a preceeding read operation
has failed. So the typical read loop in C++ looks like this

while( read operation works ) {
do something with the read data
}

// read operation has failed, but why?
// use eof() to figure out if it was because of EOF
// if it was, then the file was processed entirely.
// if it was not, then something else has happened

if( eof does not return true ) {
alert user of file reading error and take
some actions
}

All of the C++ read operations support this by returning something
that can immediatly be used for checking if the operation has succeeded.
In your case:

while( getline( insertfile, insertline ) )
outfile << insertline << endl;

if( !insertfile.eof() )
cout << "Error during file copy operation";

This also has the advantage, that the reading loop terminates not only
because of eof, but also because of all sorts of errors that can happen
during reading: file corrupt, network connection breakdown, user removing
the floppy from the drive, hard disc failure, etc...
 
P

Pieter Provoost

Karl Heinz Buchegger said:
See below


It is. You just haven't noticed it :)
Compare the input file closely with the output file and you will
see that the last line is duplicated. In case the last line is an empty
line, you will find, that your output file has one empty line more then
the input file. It is easy to miss, but it is there. If you don't
believe me, make an experiment: In your input file, in the last line,
write some text but don't terminate that line with a 'return'. Then
you will see.

The point is:
eof() turns to true only after you tried *and failed* to read past
the end of file. C++ has no crystal ball, like other languages. It doesn't
try to predict the future.

eof() is ment to be used to figure out why a preceeding read operation
has failed. So the typical read loop in C++ looks like this

while( read operation works ) {
do something with the read data
}

// read operation has failed, but why?
// use eof() to figure out if it was because of EOF
// if it was, then the file was processed entirely.
// if it was not, then something else has happened

if( eof does not return true ) {
alert user of file reading error and take
some actions
}

All of the C++ read operations support this by returning something
that can immediatly be used for checking if the operation has succeeded.
In your case:

while( getline( insertfile, insertline ) )
outfile << insertline << endl;

if( !insertfile.eof() )
cout << "Error during file copy operation";

This also has the advantage, that the reading loop terminates not only
because of eof, but also because of all sorts of errors that can happen
during reading: file corrupt, network connection breakdown, user removing
the floppy from the drive, hard disc failure, etc...

Thanks a lot for your reply, I will have a very close look at it. I did the
experiment as you described it before, and in the new file the last line was
not duplicated. But I'm sure you are right if you say I'm using eof() the
wrong way, I'm just starting with C++. I'll rewrite that piece of code...

Cheers
Pieter
 
K

Karl Heinz Buchegger

Pieter said:
not duplicated. But I'm sure you are right if you say I'm using eof() the
wrong way, I'm just starting with C++. I'll rewrite that piece of code...

Wee see this all to often.
Even book authors get it wrong :)
 
P

Panjandrum

Karl said:
Wee see this all to often.
Even book authors get it wrong :)

Then the library interface is not well designed and the library should
be avoided (at least for real work with files).
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top