Jump to content

C++ Files


Clara

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

 

----- ?

Link to comment
Share on other sites

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++;

}

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

I knew I shouldn't have tried to be lazy and not test the code :P

 

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.

Link to comment
Share on other sites

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? :D

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.