473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

filestream problem

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
Jul 23 '05 #1
11 2205
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
Jul 23 '05 #2

"Victor Bazarov" <v.********@comAcast.net> schreef in bericht
news:Ya****************@newsread1.mlpsca01.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

Jul 23 '05 #3
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
kb******@gascad.at
Jul 23 '05 #4
Pieter Provoost wrote:
"Victor Bazarov" <v.********@comAcast.net> schreef in bericht
news:Ya****************@newsread1.mlpsca01.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
Jul 23 '05 #5

"Karl Heinz Buchegger" <kb******@gascad.at> schreef in bericht
news:42***************@gascad.at...
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
Jul 23 '05 #6
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
kb******@gascad.at
Jul 23 '05 #7

"Karl Heinz Buchegger" <kb******@gascad.at> schreef in bericht
news:42***************@gascad.at...
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
kb******@gascad.at


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
Jul 23 '05 #8
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
kb******@gascad.at
Jul 23 '05 #9
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. :-)
Jul 23 '05 #10
Karl Heinz Buchegger wrote:
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).

Jul 23 '05 #11
Panjandrum wrote:

Karl Heinz Buchegger wrote:
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).


I don't agree.
There are 2 possibilities how eof() can be handled. C and C++
choose one of them for good reasons. One of them beeing the
principle that there should be no difference in streams no
matter where those streams are connected to, be it a file or
be it the console. And for the console (in contrast to a file)
it is impossible to predict if the user is willing to enter
more data or just signals 'end of data'.

People just have to learn that C++ doesn't try to predict
the future. eof() gets true only after a read operation has
failed and not when the current read operation has reached
the end of the stream.

Other languages do it differently. But that is not the fault of
C++.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: GDL | last post by:
Hi, I'm using a FileStream as below: FileStream fs = new FileStream ( filePath, FileMode.Create, FileAccess.Write, FileShare.None
1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
3
by: Muki Rapp | last post by:
Hi! In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code is designed to handle it. However, when the GC is invoked, it calls the Finalize of...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
3
by: Loane Sharp | last post by:
Hi there I use the FileStream object to download a zip file over the internet to my local disk. The file downloads successfully, but when I attempt to unzip it, I'm told that the file is in use...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
3
by: Hugh Janus | last post by:
Hi group, I am using a TCPStream together with a FileStream to send a file across a network. Everything works fine except for one thing. Always, at the end of the file there are several lines...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
2
by: Radek | last post by:
Hi, I have the following problem with FileStream. In this line: FileStream file = new FileStream(filePath, FileMode.Append); there is an exception FileNotFoundException. But for sure path and...
8
by: Andreas Zimmermann | last post by:
Hi, we just set up a SQL Server 2008 on Windows Server 2008 (configured as file / webserver, 16 GB memory, 4 dual core processors, 6 internal disks + 15 disc ISCSI array with overall almost 3 TB...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.