creating multiple text files

Started by
3 comments, last by Enigma 18 years, 6 months ago
in a for loop i want to creat multiple text files but i just can figure it out i used this code but it olny creates one char* basename = "textfile"; for(int q = 1; q < 1002;q++) { cout << "Creating text file" << q <<"...." <<endl; for( int i = 0; i < 1000;i++) { char filname [256] = {0}; sprintf(filname,"%s_%i.txt",basename,i); poop.open(filname); pee.open(filname); } } poop.close(); pee.close(); } but it wont work
Advertisement
Move your close() functions inside your for loop.
You can't open an already-open file object.



~BenDilts( void );
std::string basename = "textfile";std::string extension = ".txt";std::ofstream file;for (int fileNumber = 0; fileNumber < numberOfFiles; ++fileNumber){	std::string filename = basename + '_' + boost::lexical_cast< std::string >(fileNumber) + extension;	file.open(filename.c_str());	file.close();}

Enigma
I'm pretty sure the only way to make that last peice of code less effecient is to perhaps throw in an infinite loop.
Because saving a few cycles there is going to be so critical when the entire function is going to be I/O limited anyway. It's simple and it's error proof. Save the optimisation for when profiler shows need.

Enigma

This topic is closed to new replies.

Advertisement