473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

File Saving

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::open ?

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.

Oct 14 '05 #1
3 1369
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::open ?
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
Oct 14 '05 #2
Thank you, I guess tmpnam() and rename() will have to do!

Have a good weekend.

Aviv.

Oct 14 '05 #3
"Aviv Ben-Yosef" <av*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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::open ?

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

Oct 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alper Adatoz | last post by:
Hi, i have a little problem. i hope u guys give me a clear solution (: db: mssql i just want to put jpeg file to the image field at the mssql db. and after that i want to call it back..
7
by: Martin | last post by:
Again drawing on the groups experience:- 1. For general file opening and file saving, using VB6, are there any issues with using the FileOpen and FileSave Common Dialog Boxes? 2. Is using the...
7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
3
by: NuB | last post by:
I want to allow the user to upload a file to a server from my windows form and then display that file name in a grid. When the user clicks on the file name i want the user to be able to open the...
8
by: Corey B | last post by:
I am writing a web application in ASP.NET that allows a user to download an XML file to their machine that contains all of their work. Then later they can upload that file to the server and...
6
by: Mark Denardo | last post by:
My question is similar to one someone posted a few months back, but I don't see any replies. Basically I want to be able to have users upload photos and save them in a database (as byte data)...
0
by: Speilman_54 | last post by:
Hi, I'm converting an excel Macro into visual basic 2005 express, as I don't have a copy of VB 6 and trying to make and executable from it, I know this version doesn't have the save file as .exe,...
0
by: Waqas.L.Khan | last post by:
Hi guys, I have a problem when trying to create an image file. Basically my code takes any file and gets it's system icon using SHGetFileInfo and then saves the file either by converting it into...
6
by: Karl | last post by:
Hi all, It may seem like a rather odd request (or not) but I would like to be able to create a file (doc, jpg, xls or one of many other files that can be automated) on a website and stream it to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.