file creation in C++

Status
Not open for further replies.

alirezan1

Beta member
Messages
2
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
 
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.
 
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
 
Status
Not open for further replies.
Back
Top Bottom