File Saving

Discussion in 'C++' started by Aviv Ben-Yosef, Oct 14, 2005.

  1. Hello all.

    I've been wondering whether or not there's a way to start writing to an
    ofstream and then save it under a specific filename, or must it be done
    in the first hand with fstream::eek:pen ?

    If so, what is the ``cleanest'' way of doing so?
    Opening a temporary file and later changing the file's name?
    Is there a portable way of doing this?

    Thanks ahead,

    Aviv.
     
    Aviv Ben-Yosef, Oct 14, 2005
    #1
    1. Advertising

  2. Aviv Ben-Yosef wrote:
    > I've been wondering whether or not there's a way to start writing to an
    > ofstream and then save it under a specific filename, or must it be done
    > in the first hand with fstream::eek:pen ?


    It has to be done ahead of time.

    > If so, what is the ``cleanest'' way of doing so?


    Depends on your definition of 'cleanest'.

    > Opening a temporary file and later changing the file's name?


    Sounds good.

    > Is there a portable way of doing this?


    Sure. See 'tmpnam' and 'rename' functions. They are standard C.

    V
     
    Victor Bazarov, Oct 14, 2005
    #2
    1. Advertising

  3. Thank you, I guess tmpnam() and rename() will have to do!

    Have a good weekend.

    Aviv.
     
    Aviv Ben-Yosef, Oct 14, 2005
    #3
  4. "Aviv Ben-Yosef" <> wrote in message
    news:...

    > I've been wondering whether or not there's a way to start writing to an
    > ofstream and then save it under a specific filename, or must it be done
    > in the first hand with fstream::eek:pen ?
    >
    > If so, what is the ``cleanest'' way of doing so?
    > Opening a temporary file and later changing the file's name?
    > Is there a portable way of doing this?


    If the contents can fit the memory, and your implementation of ostringstream
    is not too slow, you can write to an ostringstream first, and then create
    the file and save the contents of the ostringstream.

    ostringstream contents;
    contents << /* ... */;
    ofstream file(/* ... */);
    file << contents.str();

    I think there is a faster way of achieving the last step, which involves the
    streams' buffers, not the streams themselves...

    Ali
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 15, 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. Kwaj
    Replies:
    1
    Views:
    436
    Grigorios Angelis
    Feb 27, 2004
  2. Chad A. Beckner

    IDE very slow when saving file

    Chad A. Beckner, Dec 24, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    577
    Ray Cassick \(Home\)
    Dec 24, 2005
  3. John Kandell
    Replies:
    4
    Views:
    4,170
    eeebop
    Dec 10, 2004
  4. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,523
    Luis Esteban Valencia
    Jan 6, 2005
  5. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Replies:
    1
    Views:
    211
    Curt_C [MVP]
    Oct 3, 2003
Loading...

Share This Page