Skip to main content
gamedev.net
🔒 Locked

creating multiple text files

Started by dk32321 Sep 23, 2005 at 11:18 AM 3 replies 600+ views
Original Post
dk32321
dk32321
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
BeanDog
BeanDog
Move your close() functions inside your for loop.
You can't open an already-open file object.



~BenDilts( void );
Enigma
Enigma
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
Puzzler183
Puzzler183
I'm pretty sure the only way to make that last peice of code less effecient is to perhaps throw in an infinite loop.
Enigma
Enigma
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.