ofstream problem

Discussion in 'C++' started by Gurikar, May 6, 2005.

  1. Gurikar

    Gurikar Guest

    Hello,
    I have small clafification.

    #include<fstream>
    #include<iostream>
    using namespace std;

    void main()
    {
    ofstream ofs;
    ofs<<"before open"<<endl;
    ofs.open("\\test.txt");
    ofs<<"Test File"<<endl;
    ofs.close();
    }

    when i do this, there is nothing in test.ext file. I do agree that iam
    writing something using ofstream before creating file test.txt, but it
    should give error. Even after that, iam creating file and opened, when
    i write something after, nothing is getting written in that file. It
    justs empty? Why is this behaviuor??

    Regards
    Gurikar, May 6, 2005
    #1
    1. Advertising

  2. Gurikar

    cadull Guest

    I think you will find that ofs.fail() will return true. To enable further
    use of the stream call ofs.clear(). This will reset the error flags.

    Regards,
    cadull

    "Gurikar" <> wrote in message
    news:...
    > Hello,
    > I have small clafification.
    >
    > #include<fstream>
    > #include<iostream>
    > using namespace std;
    >
    > void main()
    > {
    > ofstream ofs;
    > ofs<<"before open"<<endl;
    > ofs.open("\\test.txt");
    > ofs<<"Test File"<<endl;
    > ofs.close();
    > }
    >
    > when i do this, there is nothing in test.ext file. I do agree that iam
    > writing something using ofstream before creating file test.txt, but it
    > should give error. Even after that, iam creating file and opened, when
    > i write something after, nothing is getting written in that file. It
    > justs empty? Why is this behaviuor??
    >
    > Regards
    >
    cadull, May 6, 2005
    #2
    1. Advertising

  3. "Gurikar" <> wrote in message
    news:...
    | Hello,
    | I have small clafification.
    |
    | #include<fstream>
    | #include<iostream>
    | using namespace std;
    |
    | void main()

    You should be using a return type of int for the
    signature of main().

    | {
    | ofstream ofs;
    | ofs<<"before open"<<endl;

    You have just broken the stream state, and it will need
    to be reset with the member 'clear()'.

    | ofs.open("\\test.txt");
    | ofs<<"Test File"<<endl;
    | ofs.close();
    | }
    |
    | when i do this, there is nothing in test.ext file. I do agree that iam
    | writing something using ofstream before creating file test.txt, but it
    | should give error. Even after that, iam creating file and opened, when
    | i write something after, nothing is getting written in that file. It
    | justs empty? Why is this behaviuor??

    Because you broke the stream, and you cannot open a file
    with a broken stream until you repair it (reset the stream
    state flags), with clear().

    Cheers,
    Chris Val
    Chris \( Val \), May 6, 2005
    #3
  4. Gurikar

    Kristo Guest

    Gurikar wrote:
    > Hello,
    > I have small clafification.
    >
    > #include<fstream>
    > #include<iostream>
    > using namespace std;
    >
    > void main()
    > {
    > ofstream ofs;
    > ofs<<"before open"<<endl;
    > ofs.open("\\test.txt");
    > ofs<<"Test File"<<endl;
    > ofs.close();
    > }
    >
    > when i do this, there is nothing in test.ext file. I do agree that
    > iam writing something using ofstream before creating file test.txt,
    > but it should give error.


    No, it shouldn't. The standard says that if an error occurs while
    writing (e.g., writing to a file that isn't open) it sets failbit.
    There is no mention of an error message.

    > Even after that, iam creating file and opened, when i write something
    > after, nothing is getting written in that file. It justs empty? Why
    > is this behaviuor??


    Once failbit has been set, anything you do with a stream object will
    also fail. For this reason, always check that open didn't fail before
    you write to a file.

    Kristo
    Kristo, May 6, 2005
    #4
    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. Tom Johnson
    Replies:
    4
    Views:
    385
    red floyd
    Aug 15, 2003
  2. Armando
    Replies:
    2
    Views:
    7,055
    Martijn Lievaart
    Jan 23, 2004
  3. slyphiad

    ofstream problem

    slyphiad, Sep 14, 2004, in forum: C++
    Replies:
    2
    Views:
    2,086
    Mike Wahler
    Sep 14, 2004
  4. Alfons
    Replies:
    1
    Views:
    434
  5. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    595
    Ivan Johansen
    Jul 14, 2005
Loading...

Share This Page