Can not open file with "ios_base::in | ios_base::app"

Discussion in 'C++' started by Robin, Jan 5, 2007.

  1. Robin

    Robin Guest

    // Pls note that test.txt exists.
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main()
    {
    fstream fs("test.txt", ios_base::in | ios_base::app);
    if(!fs)
    {
    cout << "Error while opening file!" << endl;
    return 1;
    }
    string s;
    getline(fs, s);
    cout << s << endl;
    fs << "hello" << endl;
    return 0;
    }

    The output is always "Error while opening file!" even when test.txt
    exists. What's the problem?

    System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2

    Thanks very much for your attention.
    Robin, Jan 5, 2007
    #1
    1. Advertising

  2. Robin

    Lionel B Guest

    On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:

    > // Pls note that test.txt exists.
    > #include <iostream>
    > #include <fstream>
    > #include <string>
    >
    > using namespace std;
    >
    > int main()
    > {
    > fstream fs("test.txt", ios_base::in | ios_base::app);


    The ios_base::app means "append" or "Seek to end before each write", and
    only makes sense if you are opening a file for *output* (don't think it
    works if ios::in is specified). Try instead just:

    fstream fs("test.txt");

    as the default open mode for an fstream mode is ios::in|ios::eek:ut, which
    looks like what you want.

    > if(!fs)
    > {
    > cout << "Error while opening file!" << endl;
    > return 1;
    > }
    > string s;
    > getline(fs, s);


    You should test whether that read worked...

    > cout << s << endl;


    Looks like you want to append to the file. This sets the write position to
    the end of file:

    fs.seekp(0,ios::end);


    > fs << "hello" << endl;


    Again you should test whether that write worked... and you should close
    the fs once you're done.

    fs.close();

    > return 0;
    > }
    >
    > The output is always "Error while opening file!" even when test.txt
    > exists. What's the problem?
    >
    > System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2
    >
    > Thanks very much for your attention.


    --
    Lionel B
    Lionel B, Jan 5, 2007
    #2
    1. Advertising

  3. Robin

    Robin Guest

    "Lionel B дµÀ£º
    "
    > On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:
    >
    > --
    > Lionel B


    Thanks very much for reply.

    That kind of usage (ios_base::in | ios_base::app) was found in some
    books, for example, Lippman's C++ Primer 3rd Edition. Seems not
    supported by newer compilers.
    Robin, Jan 5, 2007
    #3
  4. Robin

    Lionel B Guest

    On Fri, 05 Jan 2007 05:44:35 -0800, Robin wrote:

    > "Lionel B 写é“:
    > "
    >> On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:
    >>
    >> --
    >> Lionel B

    >
    > Thanks very much for reply.
    >
    > That kind of usage (ios_base::in | ios_base::app) was found in some
    > books, for example, Lippman's C++ Primer 3rd Edition. Seems not
    > supported by newer compilers.


    Yes, I don't know what the Standard says (maybe someone can enlighten us
    on this), but it doesn't work for me here on a recent gcc. In principle
    I'm not sure why it *shouldn't* work, since read and write positions are
    separately maintained.

    --
    Lionel B
    Lionel B, Jan 5, 2007
    #4
  5. Robin wrote:
    > // Pls note that test.txt exists.
    > #include <iostream>
    > #include <fstream>
    > #include <string>
    >
    > using namespace std;
    >
    > int main()
    > {
    > fstream fs("test.txt", ios_base::in | ios_base::app);
    > if(!fs)
    > {
    > cout << "Error while opening file!" << endl;
    > return 1;
    > }
    > string s;
    > getline(fs, s);
    > cout << s << endl;
    > fs << "hello" << endl;
    > return 0;
    > }
    >
    > The output is always "Error while opening file!" even when test.txt
    > exists. What's the problem?
    >
    > System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2
    >
    > Thanks very much for your attention.


    A wild guess -- perhaps the file exists, but is
    not accessible for you to open (sharing violation,
    access rights, etc.) at the time this code is ran.

    HTH,
    - J.
    Jacek Dziedzic, Jan 5, 2007
    #5
  6. Robin

    Bo Persson Guest

    Lionel B wrote:
    > On Fri, 05 Jan 2007 05:44:35 -0800, Robin wrote:
    >
    >> "Lionel B ??:
    >> "
    >>> On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:
    >>>
    >>> --
    >>> Lionel B

    >>
    >> Thanks very much for reply.
    >>
    >> That kind of usage (ios_base::in | ios_base::app) was found in some
    >> books, for example, Lippman's C++ Primer 3rd Edition. Seems not
    >> supported by newer compilers.

    >
    > Yes, I don't know what the Standard says (maybe someone can
    > enlighten us on this), but it doesn't work for me here on a recent
    > gcc. In principle I'm not sure why it *shouldn't* work, since read
    > and write positions are separately maintained.


    Read and write positions are not separately maintained, there is just one
    "stream position". You have to do a seek to change between read and write
    modes.

    The standard contains an explicit list of possible combinations of open
    modes. The in | app combination just isn't in that list.


    Bo Persson
    Bo Persson, Jan 5, 2007
    #6
  7. Robin

    Lionel B Guest

    On Fri, 05 Jan 2007 20:19:02 +0100, Bo Persson wrote:

    > Lionel B wrote:
    >> On Fri, 05 Jan 2007 05:44:35 -0800, Robin wrote:
    >>
    >>> "Lionel B ??:
    >>> "
    >>>> On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:
    >>>>
    >>>> --
    >>>> Lionel B
    >>>
    >>> Thanks very much for reply.
    >>>
    >>> That kind of usage (ios_base::in | ios_base::app) was found in some
    >>> books, for example, Lippman's C++ Primer 3rd Edition. Seems not
    >>> supported by newer compilers.

    >>
    >> Yes, I don't know what the Standard says (maybe someone can
    >> enlighten us on this), but it doesn't work for me here on a recent
    >> gcc. In principle I'm not sure why it *shouldn't* work, since read
    >> and write positions are separately maintained.

    >
    > Read and write positions are not separately maintained, there is just one
    > "stream position". You have to do a seek to change between read and write
    > modes.


    Is that right? I stand corrected then (I thought seekg() and seekp()
    maintained separate read/write positions simultaneously).

    > The standard contains an explicit list of possible combinations of open
    > modes. The in | app combination just isn't in that list.


    Right. Thanks,

    --
    Lionel B
    Lionel B, Jan 5, 2007
    #7
  8. Robin

    BobR Guest

    Lionel B wrote in message ...
    >On Fri, 05 Jan 2007 20:19:02 +0100, Bo Persson wrote:
    >
    >>> Yes, I don't know what the Standard says (maybe someone can
    >>> enlighten us on this), but it doesn't work for me here on a recent
    >>> gcc. In principle I'm not sure why it *shouldn't* work, since read
    >>> and write positions are separately maintained.

    >>
    >> Read and write positions are not separately maintained, there is just one
    >> "stream position". You have to do a seek to change between read and write
    >> modes.

    >
    >Is that right? I stand corrected then (I thought seekg() and seekp()
    >maintained separate read/write positions simultaneously).
    >Lionel B


    Bo's statement now has me confused[1]. Is it just a GCC thing?
    Try this and see what output you get:

    #include <iostream>
    #include <sstream> // or <fstream>
    #include <string>

    int main(){
    using std::cout; // for NG post
    std::stringstream ss( "Hello World!\n" );
    cout<<" ss 1 tellp="<<ss.tellp()<<std::endl;
    cout<<" ss 1 tellg="<<ss.tellg()<<std::endl;
    ss.seekp( 0, std::ios_base::end );
    cout<<" ss 2 tellp="<<ss.tellp()<<std::endl;
    cout<<" ss 2 tellg="<<ss.tellg()<<std::endl;
    ss.seekg( 1 );
    ss << "Bite me!\n";
    cout<<" ss 3 tellp="<<ss.tellp()<<std::endl;
    cout<<" ss 3 tellg="<<ss.tellg()<<std::endl;
    std::string Line;
    std::getline( ss, Line );
    cout<<" stringstream getline(ss, Line) ="<<Line<<std::endl;
    cout<<" stringstream ss ="<<ss.str()<<std::endl;
    }

    /* - output - [ GCC MinGW 3.3.1 ]
    ss 1 tellp=0
    ss 1 tellg=0
    ss 2 tellp=13
    ss 2 tellg=0
    ss 3 tellp=22
    ss 3 tellg=1
    stringstream getline(ss, Line) =ello World!
    stringstream ss =Hello World!
    Bite me!
    */

    [1] - unless Bo was only refering to the 'std::ios_base::[ in|app ]' case.
    --
    Bob R
    POVrookie
    BobR, Jan 5, 2007
    #8
  9. Robin

    P.J. Plauger Guest

    "BobR" <> wrote in message
    news:FCBnh.649722$...

    > Lionel B wrote in message ...
    >>On Fri, 05 Jan 2007 20:19:02 +0100, Bo Persson wrote:
    >>
    >>>> Yes, I don't know what the Standard says (maybe someone can
    >>>> enlighten us on this), but it doesn't work for me here on a recent
    >>>> gcc. In principle I'm not sure why it *shouldn't* work, since read
    >>>> and write positions are separately maintained.
    >>>
    >>> Read and write positions are not separately maintained, there is just
    >>> one
    >>> "stream position". You have to do a seek to change between read and
    >>> write
    >>> modes.

    >>
    >>Is that right? I stand corrected then (I thought seekg() and seekp()
    >>maintained separate read/write positions simultaneously).
    >>Lionel B

    >
    > Bo's statement now has me confused[1]. Is it just a GCC thing?
    > Try this and see what output you get:
    >
    > #include <iostream>
    > #include <sstream> // or <fstream>
    > #include <string>
    >
    > int main(){
    > using std::cout; // for NG post
    > std::stringstream ss( "Hello World!\n" );


    stringstream maintains two seek positions, fstream maintains one.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
    P.J. Plauger, Jan 6, 2007
    #9
  10. Robin

    BobR Guest

    P.J. Plauger wrote in message ...
    >"BobR" wrote in message...
    >> Bo's statement now has me confused[1]. Is it just a GCC thing?
    >> Try this and see what output you get:
    >> #include <iostream>
    >> #include <sstream> // or <fstream>
    >> #include <string>
    >> int main(){
    >> using std::cout; // for NG post
    >> std::stringstream ss( "Hello World!\n" );

    >
    >stringstream maintains two seek positions, fstream maintains one.


    Thank you.

    --
    Bob R
    POVrookie
    BobR, Jan 6, 2007
    #10
    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. solartimba
    Replies:
    15
    Views:
    767
    red floyd
    Oct 20, 2003
  2. wax
    Replies:
    9
    Views:
    321
  3. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    178
    Rick DeNatale
    Oct 24, 2006
  4. Clare Hsiao
    Replies:
    11
    Views:
    251
    Michael Winter
    Feb 13, 2004
  5. Matt
    Replies:
    1
    Views:
    452
    Michael Winter
    Aug 22, 2004
Loading...

Share This Page