Writing the values on a txt file

Hello folks,
I need a small help for writing the varible x[i][j] at each iteration but I do not know how to do that. I have a function to update the value of x[i][j] and a main function I call this function.

int main()
{
for (int i=0, i<iter_limit,++i)
{
updateX() // I call the the function which updates the x[i][j]
}
return 0;
}

I want to write the values of x[i][j] at each iteration in a txt file. How can I do that ? Thank you so much

Hello learner999,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



You could start by showing all of your code. There is a good chance that your update function can be modified to write to a file. If not yo could at least show what you have tried and give a better idea of what you know.

Somewhere you need to define a file stream variable and open a file. This could be in "main" or in the function. Most likely in a write function.

Andy
Is there a particular format you want the txt file to look like?

If plaintext, whitespace-separated numbers are OK, then something like:
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
#include <fstream>

int main()
{
	const int M = 5;
	const int N = 7;
	int x[M][N] {};
	
	// filling in with some example values
	for (int i = 0; i < M; i++)
	for (int j = 0; j < N; j++)
	{
		x[i][j] = (i + 2)*(j + 3) % 17;
	}
	
	std::ofstream fout("file.txt");
	
	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < N; j++)
		{
			fout << x[i][j] << ' ';
		}
		fout << '\n';
	}
	
	return 0;
}


Will produce the following file.txt:
>cat file.txt
6 8 10 12 14 16 1
9 12 15 1 4 7 10
12 16 3 7 11 15 2
15 3 8 13 1 6 11
1 7 13 2 8 14 3

Last edited on
Thank you Ganado, I guess you understood what I want . I do similar thing but it Always writes the values of x[i][j] in the latest iteration . However, the format I want is in the txt file:

iteration 1
x[0][0]=1
x[1][0]=2
x[0][1]=3
x[1][1]=2

iteration 2
x[0][0]=5
x[1][0]=4
x[0][1]=6
x[1][1]=7

That's why I call the update X function in fhe for loop.
I will apprecaite any help

Thank you @Handy Andy. Since the code was very easy , I did not pay attention to the indendation; Next time I will be careful
Okay, do you know you would write x[0][0] = 1, x[1][0] = 1, etc. to the screen with cout?
cout << "x[" << i << "][" << j << "] = " << x[i][j] << '\n';

It's the same thing with a file stream.
Just replace "cout" with the name of your ofstream (in my example, fout). Try it and see.
Last edited on
Thank you @Ganado, of course , I do that , but as I said, it writes the value of the last iteration which is

iteration 2
x[0][0]=5
x[1][0]=4
x[0][1]=6
x[1][1]=7

it does not write the values of the previous iterations . Thank you
int main()
{
   ...
   ofstream fout("file.txt");   // Open the file BEFORE the iteration loop
   for (int i=0, i<iter_limit,++i)
   {
      updateX( x, ... ); // call the function which updates x[]][]
      writeX( x, fout, ... ); // write to stream fout
   }
}
Last edited on
Thank you so much @lastchance, I realized my mistake and I opened the file before for loop as you proposed. Thank you so much

Thank you everyone , the problem has been solved

Topic archived. No new replies allowed.