How to Export a .txt file from a .cpp file

I just made this simple code
and I was ask to export it to a .txt file
does anyone knows how to do that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    for(int a=1;a<11;a++)
    {
        for(int k=1;k<11;k++)
        {
            cout<<a<<'x'<<k<<'='<<a*k<<endl;
        }
    }
    return 0;
}
If I'm understanding the request right, the code is already text. You can just save it as a text file. (Though honestly, if someone is requesting a txt file of code instead of a cpp file I'd be wondering how much they know about programming.)
Sorry, I did not explain myself very well...
I want to export the output from that code to a .txt file

as you know this code gives the multiplications chart from 1 to 10
I was ask to export those results to a .txt file

I am barely learning c++ ok... thats why I am doing these simple coding...
As far as I know,
#include the fstream library
create an ofstream object which takes two arguments, a path and ios::out
put to the ofstream object
close the object using object name.close().

I'm new to c++ too, but that's how I learned it recently
Take #include <stdio.h> out.

Everything Rasta Wolf said is right.

The simple way is redirection. cout is standard output, which can be a file. You do this in the command line:
$ myProgram >output.txt
Similarly, cin can become from a file too:
$ myProgram <input.txt

Maybe you want to output to screen and file, then you need fstream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>
#include <iostream>
using namepsace std;

int main()
{
  // output file
  ofstream out ("output.txt");
  // to create a file for input: ifstream in("input.txt");
  
  // Use ofstream like cout
  out << "Hello World\n";
  cout << "I wrote data to the file\n";
 // ifstream is just like cin:

  out.close();
 return 0;
}
Last edited on
Thanks LowestOne that really help
nw i just need to learn how to make tables but that's for another Topic
Thank You all
Topic archived. No new replies allowed.