fstream::open

Discussion in 'C++' started by derek@idirect.com, Dec 8, 2005.

  1. Guest

    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::eek:pen, 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, 2005
    #1
    1. Advertising

  2. wrote:

    > same with fstream::eek:pen, 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
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Dec 8, 2005
    #2
    1. Advertising

  3. Guest

    I noticed how you avoided the real issue ;)
    , Dec 8, 2005
    #3
  4. 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:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
    Dietmar Kuehl, Dec 8, 2005
    #4
  5. Guest

    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, 2005
    #5
  6. 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:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
    Dietmar Kuehl, Dec 9, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens

    fstream.open( name, mode) fails

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    0
    Views:
    1,450
    Marc Schellens
    Jul 15, 2003
  2. Laszlo Barabas

    open a pipe /c++ fstream as return?

    Laszlo Barabas, Aug 22, 2003, in forum: C++
    Replies:
    2
    Views:
    3,893
    Alex Vinokur
    Aug 22, 2003
  3. =?ISO-8859-1?Q?Klaus_F=FCller?=

    fstat for an open fstream

    =?ISO-8859-1?Q?Klaus_F=FCller?=, Sep 26, 2003, in forum: C++
    Replies:
    4
    Views:
    4,306
    Mike Wahler
    Sep 27, 2003
  4. Armando
    Replies:
    6
    Views:
    735
    Armando
    Jan 29, 2004
  5. Manuel Maria Diaz Gomez

    fstream::open & seg fault

    Manuel Maria Diaz Gomez, Jul 20, 2004, in forum: C++
    Replies:
    3
    Views:
    1,170
    John Harrison
    Jul 20, 2004
Loading...

Share This Page