473,322 Members | 1,259 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,322 software developers and data experts.

Writing to a file

i am trying to create a program in which many different accounts are
to be created. I want each of the account's information to be stored
in a simple ".txt" file. is there anyway i can pass the contents of a
string (i.e two strings joined together e.g "account1" and ".txt") as
the name that is used when creating the file. i have previously used
ifstream/ofstream for creating a file, but with a given name set.

e.g

ofstream myfile;
myfile.open("account1.txt");
myfile << "hello";
myfile.close();

Jun 26 '07 #1
4 1315
Wilson wrote:
i am trying to create a program in which many different accounts are
to be created. I want each of the account's information to be stored
in a simple ".txt" file. is there anyway i can pass the contents of a
string (i.e two strings joined together e.g "account1" and ".txt") as
the name that is used when creating the file. i have previously used
ifstream/ofstream for creating a file, but with a given name set.

e.g

ofstream myfile;
myfile.open("account1.txt");
myfile << "hello";
myfile.close();
Try
myfile.open(yourString);

or
myfile.open("account1" + ".txt");

Cheers,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
Jun 26 '07 #2
Wilson wrote:
i am trying to create a program in which many different accounts are
to be created. I want each of the account's information to be stored
in a simple ".txt" file. is there anyway i can pass the contents of a
string (i.e two strings joined together e.g "account1" and ".txt") as
the name that is used when creating the file. i have previously used
ifstream/ofstream for creating a file, but with a given name set.

e.g

ofstream myfile;
myfile.open("account1.txt");
myfile << "hello";
myfile.close();
Yes easily enough.

string part1 = "account1";
string part2 = ".txt";
ofstream myfile;
myfile.open((part1 + part2).c_str());

The only slightly tricky bit is the need for .c_str(). That is because
open needs a C type string instead of a C++ type string.

john
Jun 26 '07 #3
Daniel Kraft wrote:
myfile.open("account1" + ".txt");
How is that supposed to work? I don't think operator+ is defined
for two const char pointers.
Jun 26 '07 #4
Juha Nieminen <no****@thanks.invalidwrote:
Daniel Kraft wrote:
>myfile.open("account1" + ".txt");

How is that supposed to work? I don't think operator+ is defined
for two const char pointers.
You are correct. It needs to be something like:

myfile.open((std::string("account1") + ".txt").c_str());

but clearer would be to separate them into variables:

std::string base = "account1";
std::string extension = ".txt";

std::string filename = base + extension;
myfile.open(filename.c_str());

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 26 '07 #5

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.