Writing ALL program output to a txt file in C++

前端 未结 5 1445
生来不讨喜
生来不讨喜 2021-01-05 02:45

I need to write all my program output to a text file. I believe it\'s done this way,

sOutFile << stdout;

where sOutFile is the ofstr

5条回答
  •  情深已故
    2021-01-05 03:11

    This is a duplicate of: this question

    You can redirect stdout, stderr and stdin using std::freopen.

    From the above link:

    /* freopen example: redirecting stdout */
    #include 
    
    int main ()
    {
      freopen ("myfile.txt","w",stdout);
      printf ("This sentence is redirected to a file.");
      fclose (stdout);
      return 0;
    }
    

    You can also run your program via command prompt like so:

    a.exe > stdout.txt 2> stderr.txt
    

提交回复
热议问题