简体   繁体   中英

How to save a C++ console output in a text file?

I have two console outputs of two different scripts. I want to compare these outputs with each other for finding conserved letters in both outputs along with their position in output. For this What I am trying to do is, I want to save both console outputs in two different text files and check those two files for conserved letters and their relative positions.

Is I'm going in correct path or their can be another option for comparing outputs. If yes then provide the method for saving console outputs in text files. And if No then provide some other accurate method for comparing console outputs.

You can use freopen to redirect the console output to a file, then use other software such as WinMerge to compare the output results.

For example:

freopen("output.txt", "w", stdout);

Then whatever you print to console, such as when using printf will be outputted to output.txt .

You could run your scripts from a C++ program, eg with popen(3) or custom exec+fork depending on whether you need to deal with escape sequence issues, etc.

for writing into file and

to read later.

You cannot directly write console output into a file. What you can do is modify your code so that every time you write to the console, you also write the same output to a FILE via fopen() .

Say for example you were to print out Output , you can store this in a string/char[] and write this out to a file.

So you must tipe:

int main()
{
    std::string A = "Helo";
    freopen("output.txt", "A", stdout);

    return 0;
}

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