Problem with fstream

Discussion in 'C++' started by Kristofer Pettijohn, Aug 19, 2004.

  1. I am trying to open a file in in+out+binary mode, and if the file
    does not exist I want to create it.. this is the snippit of code
    I am using:

    fHistory.open(filename.c_str(),
    std::ios::in | std::ios::eek:ut | std::ios::binary);

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

    if (!fHistory.is_open()) {
    throw CException("Error creating " + filename);
    }

    fHistory.write("Test", 4);

    If I delete the file specified in "filename", compile and run, it
    creates a file of zero bytes; if I run again, it writes the block
    "Test" to the file. I'm not sure what I'm doing wrong or overlooking,
    but if someone could shed some light, I would greatly appreciate it!

    Thanks!

    --
    Kristofer Pettijohn
     
    Kristofer Pettijohn, Aug 19, 2004
    #1
    1. Advertising

  2. Kristofer Pettijohn

    Fraser Ross Guest

    I'm not sure what you want. Do you want to truncate an existing file if
    there is one and open a new file otherwise? Are you closing any file
    opened?

    Fraser.
     
    Fraser Ross, Aug 20, 2004
    #2
    1. Advertising

  3. Fraser Ross <fraserATmembers.v21.co.unitedkingdom> wrote:
    > I'm not sure what you want. Do you want to truncate an existing file if
    > there is one and open a new file otherwise? Are you closing any file
    > opened?


    I want to create a file if it does not exist, and if it exists, I want
    to open it for random seeking with binary reads and writes.

    I used ios::trunc to create the file if it did not exist, since there
    is no similar way to have fstream create a file "if it doesn't exist"
    as there is in C with the fopen flag "a+" (which is what I am trying
    to replicate).

    --
    Kristofer Pettijohn
     
    Kristofer Pettijohn, Aug 20, 2004
    #3
  4. Kristofer Pettijohn

    Fraser Ross Guest

    > I used ios::trunc to create the file if it did not exist, since there
    > is no similar way to have fstream create a file "if it doesn't exist"
    > as there is in C with the fopen flag "a+" (which is what I am trying
    > to replicate).


    std::trunc is an open mode for existing files which does nothing for
    non-existing files. If a file doesn't exist the a+ append flag would be
    meaningless.

    Fraser.
     
    Fraser Ross, Aug 20, 2004
    #4
  5. Kristofer Pettijohn <> wrote:
    > If I delete the file specified in "filename", compile and run, it
    > creates a file of zero bytes; if I run again, it writes the block
    > "Test" to the file. I'm not sure what I'm doing wrong or overlooking,
    > but if someone could shed some light, I would greatly appreciate it!


    FWIW: I forgot to call fHistory.clear() after the first open failed..
    all works well now..

    void History::eek:penHistory(void) {
    std::cerr << "Opening history file " << filename << std::endl;

    fHistory.open(filename.c_str(),
    std::ios::in | std::ios::eek:ut | std::ios::binary |
    std::ios::ate);

    if (!fHistory.is_open()) {
    fHistory.clear();
    fHistory.open(filename.c_str(), std::ios::in | std::ios::eek:ut | std::ios:
    :binary | std::ios::trunc);
    if (!fHistory.is_open()) {
    throw CException("Error creating " + filename);
    }
    }
    }

    --
    Kristofer Pettijohn
     
    Kristofer Pettijohn, Aug 22, 2004
    #5
    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. las

    fstream problem

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

    Fstream problem

    jbruno4000, Nov 2, 2003, in forum: C++
    Replies:
    5
    Views:
    462
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Nov 3, 2003
  3. Brandon McCombs
    Replies:
    8
    Views:
    484
    Dave O'Hearn
    Nov 24, 2003
  4. Frédéric Manzanares

    problem with fstream

    Frédéric Manzanares, Nov 28, 2003, in forum: C++
    Replies:
    3
    Views:
    513
  5. Armando
    Replies:
    6
    Views:
    747
    Armando
    Jan 29, 2004
Loading...

Share This Page