Write to file

Hello everyone;

I am writing a code using for loop from i=0 to i=100 step 1. Every step I do the calculation I need to write the answer into text file so I want to end with 100 text files for the total calculation.

I don't know how to do that, any one can help?

Thank folks
Wait, are you wanting it to output 100 files or 100 lines to a single text file?
If the latter then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
     ofstream outfile;
     outfile.open("output.txt");

     for (int i = 0; i < 100; i++)
     {
           outfile << calculations << '\n';
     }
     
     outfile.close();
     return 0;
}

If I understand what you are wanting.
Last edited on by closed account z6A9GNh0
Thanks for you answer , but actually I need the calculated data at each step to be written in a file = I need 100 files at the end of calculation
Playing with that, but I fear that is a little more complex than I'm use to doing with file output.
I am writing a code using for loop from i=0 to i=100 step 1. Every step I do the calculation I need to write the answer into text file so I want to end with 100 text files for the total calculation.


Do the calculation. Open a file. Write the result to the file. Close the file.
Repeat.

Which part of that do you need help with that wasn't covered already?
hello cire ,

Asyou explain, this mean the file will be overwrite with each time step and that what I don NOT want.

I need everytime to write to a New file so I can end with 100 files.
I didn't say "open the file." I said "open a file."

Obviously this will be different for every repetition. Clearly you know what to do. What is the "how" that you don't understand?
Why are you needing 100 files though? Why can't you have all 100 in a single file as it would be in order and still easy to see which step it was.

Doing the 100 files would end up either requiring a lot of useless lines of code or require a few complex functions and loops to accomplish 100 files because you would have to set up the program to auto name the 100 files itself. Unless you plan to enter the 100 file names yourself.

I forgot about this method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <cstdio>
#include <fstream>
using namespace std;

int main()
{
	ofstream outfile;
	char filename[14];
	for(int i = 1; i < 101; i++)
	{
		sprintf(filename, "test%d.txt", i);
		outfile.open(filename);
		outfile << i;
		outfile.close();
	}
	return 0;
}
Last edited on by closed account z6A9GNh0
This is the way I would do it, but I think Specter might have done it better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

string convert(int x)
{
	stringstream s;
	s<<x<<".txt";
	return s.str();
}

int main()
{
     ofstream outfile;
	 
     for (int i = 0; i < 100; i++)
     {
		   
           outfile.open(convert(i+1));
           outfile << i+1 << '\n';
           cout<<convert(i+1);
           outfile.close();
		   
     }
     cin.get();
     return 0;
}
Last edited on
Topic archived. No new replies allowed.