how to write code result to txt file.

How do I print the result of this code to a Text file?

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


for(int level=0; level<LEVELS; level++) {
for(int row=0; row<ROWS; row++) {
for(int col=0; col<COLS; col++) {
if (col==0||col==3) {
cabins[level][row][col] = (row<5) ? 'B' : 'W';
} else {
cabins[level][row][col] = 'I';
}
}
}
}

for(int level=0; level<LEVELS; level++) {
cout << "--------------- LEVEL " << (level + 1) << " --------------------------" << endl;
cout << '+';
for(int col=0; col<COLS; col++) { cout << '\t' << (col+1); }
cout << endl;

for(int row=0; row<ROWS; row++) {
cout << (row + 1);
for(int col=0; col<COLS; col++) {
cout << '\t' << cabins[level][row][col];
}
cout << endl;
}
cout << endl;
}
system ("pause");
return 0;
}
closed account (1yR4jE8b)
Is doing it with C++ a requirement? If it isn't you can just redirect the shell output.

Let's say your executable is named a.exe

at the command prompt:

a.exe > somefile.txt

will take any output from a.exe and put it in a file name somefile.txt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
using namespace std;



int main()

{
ofstream out_data("filename.dat");

// then just replace all the cout with out_data everywhere

out_data << "whatever..";

// i think thats what you want?



return 0;
}
Topic archived. No new replies allowed.