File Saving

A

Aviv Ben-Yosef

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.
 
V

Victor Bazarov

Aviv said:
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
 
A

Aviv Ben-Yosef

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

Have a good weekend.

Aviv.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Saving Objects 10
Reuse of FILE Variable 5
[Newbee] Problem with saving a file 2
Loading/Saving a structure using <fstream> 4
Check if file exists 11
temporary file 11
file saving 4
Bare metal. 0

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top