### ofstream

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include #include using namespace std; void main() { int **a; int limit; cout<<"Enter Order Number for matrix : "; cin>>limit; a = new int *[limit]; for(int i=0;ilimit-1) c=0; } cout << "\n\nMagic Square of Order " << limit << " x " << limit << " is \n\n"; for(r=0; r

SO here is my magic square code and it's successful run as well , by the way
i wan to create a ofstream for it that store the input and the answer in the TXT file . can someone teach me please ? to store the answer in txt file

Example :
 ``123456`` ``````Enter Order Number for matrix : 3 Magic Square of order 3 x 3 is 8 1 6 3 5 7 4 9 2``````

and the file will be ofstream as in txt file as name magic.txt as
 ``1234`` ``````3 8 1 6 3 5 7 4 9 2``````
Last edited on
Hi there,

In the tutorial on this site there is a section that will show you how to do this:
http://cplusplus.com/doc/tutorial/files/

Hope that helps.

All the best,
NwN

i just don't know how to store the answer accordingly..
You're basically already doing the exact same thing, only to std::cout instead of a file:

 ``313233343536373839`` ``````cout << "\n\nMagic Square of Order " << limit << " x " << limit << " is \n\n"; for(r=0; r

I order to write that to a file at the same time, you can do:

 ``12345678910111213`` `````` ofstream os("magic.txt"); //open file for writing cout << "\n\nMagic Square of Order " << limit << " x " << limit << " is \n\n"; for(r=0; r

std::cout is an object of an output stream, used to send data to the standard ouput (screen).
std::ofstream is a file output stream, used to send data to a file.

Hope that helps.

All the best,
NwN
thanks ! it solved !
if after show the output of the matrix
and i pop out a command that

 ``1234567`` ``````do u wan to save the file? [y yes ][n no] cin>>choice; if (choice==y) { ofstream myfile; string name; }``````

how i wan to input the filename for myself and not the file name that i initialize and can store the result of it?
Well I don't want to just keep giving you the solutions, because that way you don't learn anything.

 ``12345678910111213`` `````` ofstream os("magic.txt"); //open file for writing cout << "\n\nMagic Square of Order " << limit << " x " << limit << " is \n\n"; for(r=0; r

So after you output the square, as you did before, you ask the user if he wants to save.
If yes, you ask him for a filename.
You then create an ofstream-object that opens that file. (line 1 of example)
Then you write the output to it. (lines 3-11 of example)
Finally, you close the file (line 13 of example)

Try that and if you run into problem, feel free to come back to us with the code you have tried.

All the best,
NwN
I done it d
 ``12345678910111213141516171819202122232425262728293031323334`` ``````cout<<"Do you want to save the file ?[y/n] : "; cin>>choice; cin.ignore(); if(choice=='y'||choice=='Y') { ofstream myfile; string filename; cout << "\nPlease enter a file name to write: "; getline( cin, filename ); myfile.open(filename.c_str()); myfile<>choice; }``````

Thanks for keep helping me ya !
Well done!