Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:47:34+00:00 2026-06-14T03:47:34+00:00

Suppose I’ve got code like #include <iostream> using namespace std; int main() { cout

  • 0

Suppose I’ve got code like

#include <iostream>
using namespace std;

int main() {
    cout << "Redirect to file1" << endl;
    cout << "Redirect to file2" << endl;
    return 0;
}

I want to redirect the first output to file1 and the second to file2. Is that possible?

I think in C, fclose(stdout) and reopen the stdout might help but I’m not sure how to reopen it or whether it works.

Thanks

UPDATE: What for?

I have a program A, which reads input from the user and generates corresponding output. Now I want to check whether it is correct, I have a program B which generate input for A, as well as correct output. B will generate one set of test data at a time. And I will have thousands of tests.

On my machine, a thousand times ./B > ``mktemp a.XXX`` works better than using ofstream. Using fstream for thousands of times, my hard drive light flashes crazily. But not when redirecting to temp file.

UPDATE2:

In C++, it seems that the prevailing answer is cout along with cerr.

What about C, apart from stderr, can I close stdout and reopen it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:47:36+00:00Added an answer on June 14, 2026 at 3:47 am

    You can always use the standard error stream for e.g. error messages.

    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Redirect to file1" << endl;
        cerr << "Redirect to file2" << endl;
    }
    

    For example, using the Windows [cmd.exe] command interpreter, and the Visual C++ cl compiler:

    [D:\dev\test]
    > type con >streams.cpp
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Redirect to file1" << endl;
        cerr << "Redirect to file2" << endl;
    }
    ^Z
    
    [D:\dev\test]
    > cl streams.cpp
    streams.cpp
    
    [D:\dev\test]
    > streams 1>a.txt 2>b.txt
    
    [D:\dev\test]
    > type a.txt
    Redirect to file1
    
    [D:\dev\test]
    > type b.txt
    Redirect to file2
    
    [D:\dev\test]
    > _
    

    EDIT: added colorized code and boldface emphasis.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have the code php as below: <?php include(connectdb.php); $check1 = ;
Suppose I would like to do extract the contents of matching curly braces using
Suppose, I have the following code: class Data { private: int *m_arr; int m_size;
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I created index with descending order CREATE INDEX `MyTable.MyIndex` USING BTREE ON `MyTable`
Suppose I have a data frame, df, that looks like: f t1 t2 t3
Suppose I have the following table: PERSON : ID INT NAME STRING LASTNAME STRING
suppose I have the following serial C: int add(int* a, int* b, int n)
Suppose we have n constants like: FOO = 'foo' BAR = 'bar' ... I
Suppose: struct P { P(int v); }; int v; P p = 0; //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.