473,406 Members | 2,894 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,406 software developers and data experts.

fstream::open

I see this has come up over and over again in the past... that
programmers waste hours of confusion when using a fstream object to
open, read/write, close then attempt to open another file (or the same
file) with the same object, and wonder why operations are failing.

If standard C's open/fopen functions will open a file without caring
about previous error states of the filehandle, then C++ should do the
same with fstream::open, or at the very least make it extremely clear
in all fstream documentation that you should call clear() before
calling open a second time.

In addition, it is quite annoying for C++ to block all I/O operations
upon the result of any error (for example, when reading from a stream,
if you get an invalid multibyte character, you have to call clear()
before you can do anything further).

What could be nice clean code results in being cluttered with numerous
calls to clear().

Dec 8 '05 #1
5 3124
de***@idirect.com wrote:
same with fstream::open, or at the very least make it extremely clear
in all fstream documentation that you should call clear() before
calling open a second time.


Sorry, but I don't think that the standard definition of a language must
specify how books and papers about the language must be written. People can
choose what books to read according to his previous knowledge a experience
in programming in general and the language in particular.

--
Salu2
Dec 8 '05 #2
I noticed how you avoided the real issue ;)

Dec 8 '05 #3
de***@idirect.com wrote:
I see this has come up over and over again in the past... that
programmers waste hours of confusion when using a fstream object to
open, read/write, close then attempt to open another file (or the same
file) with the same object, and wonder why operations are failing.


You might want to check the resolution to LWG issue 22:
<http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#22>.
This issue was discussed several times at varying but exhaustive
length. Personally, I consider it a non-issue as in modern C++ it
is pretty rare to reuse such objects anyway so why bother with this
case?
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 8 '05 #4
Say you had a program that was an implementation of something like the
unix program "split".

I'd assume the most efficient way to write it in C++ would be to use
the same static ofstream object to open each new output file.

Speaking of which, what is the most effiecient C++ method to generate a
dynamic filename for such a purpose as mentioned above?

Dec 8 '05 #5
de***@idirect.com wrote:
I'd assume the most efficient way to write it in C++ would be to use
the same static ofstream object to open each new output file.
For unformatted reading as split(1) does? Hardly... I would expect
better performance by using 'std::filebuf' directly, although not
much better: Probably most of the time is spent waiting for I/O
anyway. The time necessary to construct an 'ofstream' for each
fragment is probably not distinctly measurable in either situation.
Speaking of which, what is the most effiecient C++ method to generate a
dynamic filename for such a purpose as mentioned above?


You mean what is the most efficient method to create a sequence
of strings containing increasing numbers? Probably maintaining
an array of digits.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 9 '05 #6

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

Similar topics

0
by: Marc Schellens | last post by:
fstream.open( name, mode) fails fstream.fail() returns true. How can I find out what went wrong? (ie. file does not exist for reading, permissions...) thanks, marc
5
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the...
14
by: Mike - EMAIL IGNORED | last post by:
Using RH9, from this fragment: fstream theStream_; string fPath = "myFile"; int mode = O_EXCL | O_CREAT; theStream_.open(fPath.c_str(),mode); I get the failure shown below. ...
3
by: Manuel Maria Diaz Gomez | last post by:
Hi everybody, This is probably trivial, but I just can't see it. In the followinf function: /*-------------------------------------------------*/ File::loadFile(string inputFile)...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
5
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
3
by: Belebele | last post by:
The code below does not cause an assertion failure. fstream f; f.open("myfile"); assert( f.is_open() ); However, the one below does fail the assert:
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
1
by: massdeletion101 | last post by:
I'm a newbie at manipulating external files. Is there any way to specify a path to the file with C++? It thinks that the path in the file name string is filled with unrecognisable escape sequences....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.