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 9186199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:27:57+00:00 2026-06-17T19:27:57+00:00

The following code is writing both A and B to the file out.txt, with

  • 0

The following code is writing both “A” and “B” to the file “out.txt”, with the first call to open returning 3 and second call returning 4.

What I expected is for “A” to be written to the file and “B” to be written to the screen. I also expected open to return 3 in each case.

What should I do to fix the code below:

int main(int argc, char** argv)
{
    int file = open("out.txt", O_APPEND | O_WRONLY);
    if(file != 3)    return 1;

    if(dup2(file,1) < 0)    return 1;
    std::cout << "A" << std::endl;

    if(dup2(1,file) < 0)    return 1;
    std::cout << "B" << std::endl;

    file = open("out.txt", O_APPEND | O_WRONLY);
    if(file != 3)    return 1;

    return 0;
}
  • 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-17T19:27:59+00:00Added an answer on June 17, 2026 at 7:27 pm

    Commenting from this link;

    if(dup2(file,1) < 0)    return 1;
    

    makes newfd be the copy of oldfd, closing newfd first if necessary`

    that is, it closes stdout and makes file descriptor 1 a clone of file descriptor 3.

    if(dup2(1,file) < 0)    return 1;
    

    If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.

    So it does nothing since file descriptor 1 has the same value as file descriptor 3.

    file = open("out.txt", O_APPEND | O_WRONLY);
    

    will open a file with the next available file descriptor. Since file descriptor 3 is busy, it will (in this case) use 4.

    What you want to do is something more along the lines of;

    int stdoutCopy = dup(1);                // Clone stdout to a new descriptor
    if(dup2(file, 1) < 0) return 1;         // Change stdout to file
    close(file);                            // stdout is still valid
    std::cout << "A" << std::endl;
    if(dup2(stdoutCopy,1) < 0) return 1;    // Change stdout back from the clone
    close(stdoutCopy);                      // Close the clone
    std::cout << "B" << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing the following code to find out the required strings are present
I'm writing an XML file with the following code: Source source = new DOMSource(rootElement);
I got tired of writing the following code: /* Commenting out irrelevant parts public
I have the following code for writing draw calls to a back buffer canvas,
How I'm stuck with writing a test for the following code. I want to
Mono won't fire the following code: I get internal server error 500, error writing
does any one know if, for example, I'm writing in c++ the following code:
I am writing code to performance test a web site. I have the following
I have the following section of code in an app that I am writing:
I use the following C-ish indentation/bracing style when writing LaTeX code: \foo{ blah blah...

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.