Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > filestream problem

Reply
Thread Tools

filestream problem

 
 
Pieter Provoost
Guest
Posts: n/a
 
      06-15-2005
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-15-2005
Pieter Provoost wrote:
> 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
 
Reply With Quote
 
 
 
 
Pieter Provoost
Guest
Posts: n/a
 
      06-15-2005

"Victor Bazarov" <(E-Mail Removed)> schreef in bericht
news:YaXre.1625$(E-Mail Removed)01.us.to.v erio.net...
> Pieter Provoost wrote:
> > 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.



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



 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-15-2005
Pieter Provoost wrote:
>
> 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?")

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-15-2005
Pieter Provoost wrote:
> "Victor Bazarov" <(E-Mail Removed)> schreef in bericht
> news:YaXre.1625$(E-Mail Removed)01.us.to.v erio.net...
> [..] 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
 
Reply With Quote
 
Pieter Provoost
Guest
Posts: n/a
 
      06-16-2005

"Karl Heinz Buchegger" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> Pieter Provoost wrote:
> >
> > 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?")
>
> --
> Karl Heinz Buchegger



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

Pieter


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-16-2005
Pieter Provoost wrote:
>
> >
> > 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?")
> >
> > --
> > Karl Heinz Buchegger

>
> "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...

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Pieter Provoost
Guest
Posts: n/a
 
      06-16-2005

"Karl Heinz Buchegger" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> Pieter Provoost wrote:
> >
> > >
> > > 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?")
> > >
> > > --
> > > Karl Heinz Buchegger

> >
> > "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...
>
> --
> Karl Heinz Buchegger
> (E-Mail Removed)


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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-16-2005
Pieter Provoost wrote:
>
> 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


--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-16-2005
Karl Heinz Buchegger wrote:
> Pieter Provoost wrote:
>
>>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


Book authors are people, aren't they? I mean, most often, that is.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FileStream file name problem. Frank ASP .Net 0 05-07-2008 06:10 PM
Problem with FileStream - FileNotFoundException Radek ASP .Net 4 11-29-2007 05:15 PM
Filestream ifstream.seekg(int) problem in Borland Turbo C++ Tim Müller C++ 2 02-05-2007 05:28 PM
Problem creating a Bitmap from an IO.FileStream Nathan Sokalski ASP .Net 7 12-13-2005 09:45 AM
FileStream permission problem with asp.NET web application! =?Utf-8?B?TWlrYWVsIEd1c3RhZnNzb24=?= ASP .Net 4 05-28-2004 06:39 AM



Advertisments