Ouput to a list of numbered files

I'm trying to write a code that will output to a list of files text which is almost identical, apart from that file's number.

In short, I need to write the files:
Output1.txt, Output2.txt, Output3.txt ...

With text along the lines of:
"Output1.txt: This is file 1."
"Output2.txt: This is file 2."
"Output3.txt: This is file 3."
etc.

My code so far is below. I don't know how to insert the incrementing integer "i" into the filename so clearly all I get from this is the file "Output.txt" with the text "Output10.txt: This is file 10."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

using namespace std;


int main() {
int i=0;
while (i++ <10) {

ofstream file("Output.txt");
  
if (file.is_open()) { 
    file << "Output" << i << ".txt: This is file" << i << ".\n ";
    file.close(); 
  } else { cout << "File won't open"; }
  }
 return 0;
}



Thanks!

Last edited on
Why are you using a while loop instead of a for? In any case, something like this should work.

1
2
3
std::stringstream ss;
ss<<"Output"<<i<<".txt";
ofstream file(ss.str().c_str());
Hullo Zhuge, thanks for the reply. I tried what you suggested but get an error message when compiling:

error: aggregate `std::stringstream ss' has incomplete type and cannot be defined


Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
int i=0;
while (i++ <10) {

  std::stringstream ss;
  ss<<"Output"<<i<<".txt";
  ofstream file(ss.str().c_str());
  
if (file.is_open()) { 
    file << "Output" << i << ".txt: This is file" << i << ".\n ";
    file.close(); 
  } else { cout << "File won't open"; }
  }
 return 0;
}


Last edited on
It works. Thanks Zhuge! I forgot to include at the start of the code:

#include <sstream>

By the way, (this might be a stupid question but I've not much C++ experience) why is it better to use a for loop, rather than my while?

Thanks again
I'm kinda curious to, I don't think it particularly matters in this case, although mixing unary and assignment operators is a big no no as results are undefined.
the for loop just looks prettier and is typically used in incrementing loops, whereas while is mostly sentinel.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
   for (int i=0; i<10; i++) {
      std::stringstream ss;
      ss<<"Output"<<i<<".txt";
      ofstream file(ss.str().c_str());
  
      if (file.is_open()) { 
         file << "Output" << i << ".txt: This is file" << i << ".\n ";
         file.close(); 
      } else { cout << "File won't open"; }
   }
   return 0;
}
Last edited on
For anyone who might find this useful (I know it's a template I'll use again) here is the full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;


int main() {
for (int i=1; i<=10; i++) {
  std::stringstream ss;
  ss<<"Output"<<i<<".txt";
  ofstream file(ss.str().c_str());
  
  if (file.is_open()) { 
    file << "Output" << i << ".txt: This is file" << i << ".\n ";
    file.close(); 
    } else { cout << "File won't open"; }
  }
 return 0;
}
Topic archived. No new replies allowed.