简体   繁体   中英

How to take a c++ console application output and write it into a text file

I have created a basic console application. I would like to know how I could take the output and write it into a text file.

There are several ways to do this.

(1) You can use the command line to redirect the output of your program to a file. You would do this in Windows from the shell as

madlib-program.exe > outputFile

and in Mac/Linux from the command line as

./madlib-program > outputFile

(2) You could replace all of your program's output calls with file writing operations. For example, if you were using streaming IO, you could start the program by opening an output file:

ofstream out("output-file.txt");

and then replace all usage of cout with out :

cout << "Hello, world" << endl;

becomes

out << "Hello, world!" << endl;

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM