Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 04-05-2006, 04:13 PM   #1 (permalink)
 
Newb Techie

Join Date: Apr 2006

Posts: 2

alirezan1

Default file creation in C++

Hi guys,

I want to write a code in C++ that creates files with different filenames like this:

file1: test1.txt
file2: test2.txt
...
file38: test38.txt
...

It has to be in a loop that generates different file eachtime.

can anybody help?

thanks
alirezan1 is offline  
Old 04-05-2006, 05:02 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

Code:
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

string itos(int i){
	stringstream s;
	s << i;
	return s.str();
}

int main(){
	string fname = "file", ext = ".txt";
	int someNum = 10;
	for (int i=0; i<someNum; i++){
		fname += itos(i) + ext;
		ofstream out(fname.c_str());
		out.close();
		fname = fname.substr(0,4);
	}
		  
	return 0;
}
This works, but it's pointless.
jaeusm is offline  
Old 04-05-2006, 10:03 PM   #3 (permalink)
 
Newb Techie

Join Date: Apr 2006

Posts: 2

alirezan1

Default

Hi,

thanks for the code, but it is not pointless. I have to do a project that requires this feature(I have no choice but to do what project owner wants!)

Thank you very very much for the code.

Ali
alirezan1 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On