fstream vs ofstream

Discussion in 'C++' started by chiku, May 4, 2009.

  1. chiku

    chiku Guest

    Hi,

    When i tried below code it seems i am getting error in open method but
    same program when i tried with ofstream object insted of fstream i got
    success . Their was NO already existing file with passed name . Any
    idea why?

    Chiku

    #include<iostream>
    #include<fstream>

    using namespace std;
    int main(void)
    {

    fstream mystream;
    mystream.open("testdirect.txt");
    if(mystream.is_open())
    {
    cout << "file open success"<<"\n";
    }
    else
    {
    cout << "failed"<<"\n";
    }

    return 0;

    }
    chiku, May 4, 2009
    #1
    1. Advertising

  2. chiku

    Jim Langston Guest

    "chiku" <> wrote in message
    news:...
    > Hi,
    >
    > When i tried below code it seems i am getting error in open method but
    > same program when i tried with ofstream object insted of fstream i got
    > success . Their was NO already existing file with passed name . Any
    > idea why?
    >
    > Chiku
    >
    > #include<iostream>
    > #include<fstream>
    >
    > using namespace std;
    > int main(void)
    > {
    >
    > fstream mystream;
    > mystream.open("testdirect.txt");
    > if(mystream.is_open())
    > {
    > cout << "file open success"<<"\n";
    > }
    > else
    > {
    > cout << "failed"<<"\n";
    > }
    >
    > return 0;


    fstream is an input/output stream. The constructor (from what I"m seeing)
    takes a 2nd paramater specifying how to open it. I.E. app, ate, binary,
    in, out, trunc. An example showing usage is:

    // using fstream constructors.
    #include <iostream>
    #include <fstream>
    using namespace std;

    int main () {

    fstream filestr ("test.txt", fstream::in | fstream::eek:ut);

    // >> i/o operations here <<

    filestr.close();

    return 0;
    }

    Notice: your code is using .open This code is using the constructor. Your
    code does not specify if the file exists or not, how to open it, etc.. this
    code does.

    I've never had much luck using .open myself and have always used the
    constructor.
    Jim Langston, May 5, 2009
    #2
    1. Advertising

  3. Jim Langston wrote:
    > "chiku" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> When i tried below code it seems i am getting error in open method but
    >> same program when i tried with ofstream object insted of fstream i got
    >> success . Their was NO already existing file with passed name . Any
    >> idea why?
    >>
    >> Chiku
    >>
    >> #include<iostream>
    >> #include<fstream>
    >>
    >> using namespace std;
    >> int main(void)
    >> {
    >>
    >> fstream mystream;
    >> mystream.open("testdirect.txt");
    >> if(mystream.is_open())
    >> {
    >> cout << "file open success"<<"\n";
    >> }
    >> else
    >> {
    >> cout << "failed"<<"\n";
    >> }
    >>
    >> return 0;

    >
    > fstream is an input/output stream. The constructor (from what I"m
    > seeing) takes a 2nd paramater specifying how to open it. I.E. app,
    > ate, binary, in, out, trunc. An example showing usage is:
    >
    > // using fstream constructors.
    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    >
    > int main () {
    >
    > fstream filestr ("test.txt", fstream::in | fstream::eek:ut);
    >
    > // >> i/o operations here <<
    >
    > filestr.close();
    >
    > return 0;
    > }
    >
    > Notice: your code is using .open This code is using the constructor.
    > Your code does not specify if the file exists or not, how to open it,
    > etc.. this code does.
    >
    > I've never had much luck using .open myself and have always used the
    > constructor.


    It is the same, except you can use exceptions with open (not sure if it
    is possible with constructor), like this:

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

    int main () {

    fstream filestr;
    filestr.exception( fstream::eofbit |
    fstream::failbit |
    fstream::badbit );
    try
    {
    filestr.open("test.txt", fstream::in | fstream::eek:ut);
    }
    catch ( fstream::failure &e)
    {
    cout << "Exception opening/reading file";
    }


    // >> i/o operations here <<

    filestr.close();

    return 0;
    }
    Vladimir Jovic, May 5, 2009
    #3
  4. Vladimir Jovic wrote:
    > Jim Langston wrote:
    >> "chiku" <> wrote in message
    >> news:...
    >>> Hi,
    >>>
    >>> When i tried below code it seems i am getting error in open method but
    >>> same program when i tried with ofstream object insted of fstream i got
    >>> success . Their was NO already existing file with passed name . Any
    >>> idea why?
    >>>


    Forgot to answer the question. The reason can be anything - file
    missing, not enough access, opening read only file for writing, etc.
    Vladimir Jovic, May 5, 2009
    #4
  5. chiku

    James Kanze Guest

    On May 5, 12:37 am, chiku <> wrote:

    > When i tried below code it seems i am getting error in open
    > method but same program when i tried with ofstream object
    > insted of fstream i got success . Their was NO already
    > existing file with passed name . Any idea why?


    The mode parameter of ofstream defaults to ios::eek:ut. The mode
    parameter of fstream defaults to ios::eek:ut | ios::in. Just
    ios::eek:ut says to create a new file, or truncate an existing one.
    ios::eek:ut | ios::in says to open an existing file, without
    modifying its contents.

    The open modes are defined by mapping them to the mode arguments
    of fopen, so it's not that clean.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 5, 2009
    #5
  6. chiku

    James Kanze Guest

    On May 5, 1:15 am, "Jim Langston" <> wrote:
    > "chiku" <> wrote in message
    > news:...
    > > When i tried below code it seems i am getting error in open method but
    > > same program when i tried with ofstream object insted of fstream i got
    > > success . Their was NO already existing file with passed name . Any
    > > idea why?


    > > #include<iostream>
    > > #include<fstream>


    > > using namespace std;
    > > int main(void)
    > > {
    > > fstream mystream;
    > > mystream.open("testdirect.txt");
    > > if(mystream.is_open())
    > > {
    > > cout << "file open success"<<"\n";
    > > }
    > > else
    > > {
    > > cout << "failed"<<"\n";
    > > }
    > > return 0;


    > fstream is an input/output stream. The constructor (from what
    > I"m seeing) takes a 2nd paramater specifying how to open it.


    Both the constructor and the open function have default
    arguments for the second parameter.

    > I.E. app, ate, binary, in, out, trunc. An example showing
    > usage is:


    > // using fstream constructors.
    > #include <iostream>
    > #include <fstream>
    > using namespace std;


    > int main () {


    > fstream filestr ("test.txt", fstream::in | fstream::eek:ut);


    Those are the defaults. This changes nothing with respect to
    what he has done.

    > // >> i/o operations here <<


    > filestr.close();
    > return 0;
    > }


    > Notice: your code is using .open This code is using the
    > constructor. Your code does not specify if the file exists or
    > not, how to open it, etc.. this code does.


    > I've never had much luck using .open myself and have always
    > used the constructor.


    What's the difference? All the constructor does (in addition to
    what the default constructor does) is call open.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 5, 2009
    #6
  7. chiku

    Paul Carter Guest

    On May 5, 5:15 am, James Kanze <> wrote:
    > On May 5, 1:15 am, "Jim Langston" <> wrote:
    >
    >
    >
    > > Notice: your code is using .open  This code is using the
    > > constructor.  Your code does not specify if the file exists or
    > > not, how to open it, etc.. this code does.
    > > I've never had much luck using .open myself and have always
    > > used the constructor.

    >
    > What's the difference?  All the constructor does (in addition to
    > what the default constructor does) is call open.
    >


    He may have been trying to reuse an iostream by reopening it using
    ..open and not resetting the flags. I've seen several people run
    into problems where they have read to EOF, then try to
    reuse the stream object using .open and being surprised that the
    object state wasn't reset.

    --
    Paul
    Paul Carter, May 5, 2009
    #7
    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. m vaughn
    Replies:
    0
    Views:
    422
    m vaughn
    Jul 11, 2003
  2. Armando
    Replies:
    6
    Views:
    747
    Armando
    Jan 29, 2004
  3. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    598
    Ivan Johansen
    Jul 14, 2005
  4. bballmitch

    fstream and ofstream

    bballmitch, Sep 1, 2005, in forum: C++
    Replies:
    2
    Views:
    474
    John Harrison
    Sep 1, 2005
  5. Ramesh
    Replies:
    5
    Views:
    775
    Ramesh
    Oct 17, 2008
Loading...

Share This Page