Clara Posted March 18, 2005 Posted March 18, 2005 Hi! Do any of you know how to automatically number the names of output files when programming with C++? I mean, if I was using a loop, and wanted to output some text into a .txt file, how can I make it name the first one 1.txt. Then when it goes back into the loop, create a new file called 2.txt, and keep going until I'm done running the loop? Please let me know! Thanks! ~Clara
timo Posted March 19, 2005 Posted March 19, 2005 Count the number of times you run the loop, convert this number to a string, use the string as the name for the output file.
Clara Posted March 19, 2005 Author Posted March 19, 2005 I tried that. I need the file name to be C:/(foldername)/1.txt then C:/(foldername)/2.txt and keep going for however many times I run the loop. When I try to declare the whole file name as a string, and put the file name in quotation marks, it just saves it as the name of the string variable. When I don't use quotation marks, I get an error about an invalid function.
InovFX Posted March 19, 2005 Posted March 19, 2005 Do you mean like this : #include <iostream> #include <fstream> #include <stdlib.h> // So we can use system("pause"); using namespace std; int main() { char* filename[10]; filename[1] = "1.txt"; filename[2] = "2.txt"; filename[3] = "3.txt"; filename[4] = "4.txt"; filename[5] = "5.txt"; filename[6] = "6.txt"; filename[7] = "7.txt"; filename[8] = "8.txt"; filename[9] = "9.txt"; filename[10] = "10.txt"; for (int i = 0; i <= 10; i++) { ofstream fileout; fileout.open(filename[i]); fileout.close(); } system("pause"); return 0; } ----- ?
mossoi Posted March 20, 2005 Posted March 20, 2005 That assumes a known amount of files though, the filename needs to be built based on the occurence of the loop. I don't know any C but in PHP it would be something like this: // Reset count $i = 1; while (criteria for while loop) { // Assign value to $filename $filename = "C:\[directoryName]\" . $i . "\.txt"; [do something with the file name] // Increment the count $i++; }
Dave Posted March 20, 2005 Posted March 20, 2005 Modified the code above a bit: #include <iostream> #include <fstream> #include <string> //i'm using stdlib c++ strings because i'm lazy using namespace std; int main() { int n = 10; string temp; for (int i = 0; i <= n; i++) { ofstream fileout; temp = i + ".txt"; fileout.open(temp.c_str()); fileout.close(); } return 0; }
InovFX Posted March 21, 2005 Posted March 21, 2005 Sorry dave, I've tried it on GCC 3.4.2, and the result was strange. It's not create like "1.txt", "2.txt", "3.txt", etc... temp = i + ".txt"; This will convert the integer temp to string.... So, the result become strange....
InovFX Posted March 21, 2005 Posted March 21, 2005 This will convert the integer temp to string.... Soryy, this above should be : This will conver the integer t to a string....
Dave Posted March 21, 2005 Posted March 21, 2005 I knew I shouldn't have tried to be lazy and not test the code Try this instead: #include <iostream> #include <fstream> #include <stdio.h> using namespace std; int main() { int n = 10; char temp[20]; for (int i = 1; i <= n; i++) { ofstream fileout; sprintf(temp, "%d.txt", i); fileout.open(temp); fileout.close(); } return 0; } Works for me.
jcarlson Posted March 22, 2005 Posted March 22, 2005 I like Dave's Idea. Using streams to convert the int values to text would be easiest.
InovFX Posted March 22, 2005 Posted March 22, 2005 Thank's dave !! Now, I know how to use sprintf(); !!
Dave Posted March 26, 2005 Posted March 26, 2005 Yes, if you want to use streams, it's quite easy (and very powerful): #include <iostream> #include <sstream> #include <fstream> using namespace std; int main() { int n = 10; string temp; for (int i = 1; i <= n; i++) { stringstream mystream; string temp; ofstream fileout; mystream << i << ".txt"; temp = mystream.str(); fileout.open(temp.c_str()); fileout.close(); } return 0; } Good, init?
Dave Posted March 27, 2005 Posted March 27, 2005 (Just noticed; you can take the first string temp out, it's a bit pointless).
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now