fstream..

Discussion in 'C++' started by dumboo, May 17, 2004.

  1. dumboo

    dumboo Guest

    hi there
    i have been trying to open file using fstream, in read+write+binary mode

    fstream ff;

    ff.open(filename, ios::in | ios::eek:ut | ios::binary);

    now this works

    what i m looking for is, if the file does not exist, it should be created,
    and i dont know what ios flag to use, the above statement will open the file
    only if it already exist, for now what i m doing is chaek wether the file
    already exist, if it does not exist then it opens file just for writing and
    creates a zero byte file and then closes, then i m able to use above
    statement for writing and reading from it, m i doing it right, any
    suggestion ??

    regards
     
    dumboo, May 17, 2004
    #1
    1. Advertising

  2. "dumboo" <> wrote in message
    news:...
    > hi there
    > i have been trying to open file using fstream, in read+write+binary mode
    >
    > fstream ff;
    >
    > ff.open(filename, ios::in | ios::eek:ut | ios::binary);
    >
    > now this works
    >
    > what i m looking for is, if the file does not exist, it should be created,
    > and i dont know what ios flag to use, the above statement will open the

    file
    > only if it already exist, for now what i m doing is chaek wether the file
    > already exist, if it does not exist then it opens file just for writing

    and
    > creates a zero byte file and then closes, then i m able to use above
    > statement for writing and reading from it, m i doing it right, any
    > suggestion ??
    >
    > regards
    >


    There's no one set of flags that will do what you want. Try this

    ff.open(filename, ios::in | ios::eek:ut | ios::binary);
    if (!ff.is_open())
    {
    ff.open(filename, ios::in | ios::eek:ut | ios::trunc | ios::binary);
    if (!ff.is_open())
    error();
    }

    The second open call will create a zero length file, open for reading and
    writing.

    john
     
    John Harrison, May 17, 2004
    #2
    1. Advertising

  3. dumboo

    dumboo Guest

    hi there
    "John Harrison" <> wrote in message
    news:...
    [..]
    > There's no one set of flags that will do what you want. Try this
    >
    > ff.open(filename, ios::in | ios::eek:ut | ios::binary);
    > if (!ff.is_open())
    > {
    > ff.open(filename, ios::in | ios::eek:ut | ios::trunc | ios::binary);
    > if (!ff.is_open())
    > error();
    > }
    >

    [..]
    thats wt i m doing now, it was strange to read that there is no set of flags
    for doing such a common operation, any way thankx for your suggestion :)
    regards
     
    dumboo, May 17, 2004
    #3
  4. dumboo

    Chris Guest

    dumboo <> wrote:
    > thats wt i m doing now, it was strange to read that there is no set of flags
    > for doing such a common operation, any way thankx for your suggestion :)
    > regards


    If you compare the available flags with standard C's fopen() family of calls,
    you'll notice a striking similarity. :) fopen() doesn't have this feature
    either.

    I was surprised to find this as well, but I was so used to POSIX open() that
    it caught me off guard.

    - Chris
     
    Chris, May 17, 2004
    #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. Marc Schellens

    vector of fstream

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    2
    Views:
    396
    John Harrison
    Jul 16, 2003
  2. Marc Schellens

    fstream.open( name, mode) fails

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    0
    Views:
    1,467
    Marc Schellens
    Jul 15, 2003
  3. las

    fstream problem

    las, Jul 16, 2003, in forum: C++
    Replies:
    1
    Views:
    437
    Simon Saunders
    Jul 16, 2003
  4. gukn9700

    Weird thing with fstream

    gukn9700, Jul 17, 2003, in forum: C++
    Replies:
    0
    Views:
    336
    gukn9700
    Jul 17, 2003
  5. Armando
    Replies:
    6
    Views:
    752
    Armando
    Jan 29, 2004
Loading...

Share This Page