By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

C++ Files Generation

P: 19
Hi Friends

I am writing a program in c++ where in i will extract a word (AB2345) from a input file and then i have to create a filename by this word , i.e AB2345.txt . Like this there are around 100
words with different numbers (eg, AB123,AB521......), i.e i have to create 100 files .

How should i implement this using fstream . Please Guide Me.


Thanks in Advance
Praveen
Mar 7 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
Hi Friends

I am writing a program in c++ where in i will extract a word (AB2345) from a input file and then i have to create a filename by this word , i.e AB2345.txt . Like this there are around 100
words with different numbers (eg, AB123,AB521......), i.e i have to create 100 files .

How should i implement this using fstream . Please Guide Me.


Thanks in Advance
Praveen
what is the exact problem? how to do file IO in C++? see
http://www.cplusplus.com/doc/tutorial/files.html

how to manipulate strings to form file names? see
http://www.cplusplus.com/doc/tutorial/ntcs.html
Mar 7 '07 #2

P: 19
what is the exact problem? how to do file IO in C++? see
http://www.cplusplus.com/doc/tutorial/files.html

how to manipulate strings to form file names? see
http://www.cplusplus.com/doc/tutorial/ntcs.html

I have a file in which there are a list of keywords like
AB123
AB124
AB125
AB126
AB127
AB128
AB129
AB130
AB131
AB132
AB133

Then i have to extract each keyword for example AB123 and create a file with name AB123.txt (and insert some other information which i will do).

So i needed help in creating file names .In C++ i am creating first filename as follows

string substring="AB123";
string Path="/root/praveen/";
string ext=".txt";
string filename=Path+substring+ext;
ofstream Myfile;
Myfile.open(filename.c_str());

So this type i create first file AB123.txt



Next how to create Second file AB124.txt . Should i use a for loop and take new file handlers each time in the loop for creation of a file.

Finally as solution i have to get the following files to be created:

AB123.txt
AB124.txt
AB125.txt
AB126.txt
AB127.txt
AB128.txt
AB129.txt
AB130.txt
AB131.txt
AB132.txt
AB133.txt



Please Help me



Thanks in Advance
Praveen
Mar 7 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
You will have to use ifstream to get the keyword - using getline(ifstreamVar, strVar) will accomplish this. Then you can add the filename root as before, and finally open an ofstream object (create a file) using the .c_str() of the final product.
Mar 7 '07 #4

P: 19
Hi Friends

Thank you for ur suggestions. The problem got solved


praveen
Mar 8 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
Glad to hear it!
Mar 8 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.