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

  • SEARCH
  • Home
  • 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 3241034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:09:09+00:00 2026-05-17T18:09:09+00:00

I asked about pipes in a previous question, got that working perfectly. However I

  • 0

I asked about pipes in a previous question, got that working perfectly. However I had some questions about output redirection like >> in a shell normally does. There doesn’t seem to be a whole lot of info on the Internet about it. Here is what I have so far. Is there a better/easier way to do this, it’s messy and im not even super sure that I understand it. Part of it was psuedocode given in notes, i kinda filled them in, but even im not super sure.

void do_redirect(char** cmd, char** file) {
  int fds[2]; 
  int count; 
  int fd;     
  char i;    
  pid_t pid;  
  pipe(fds);
  //File Descriptors/pipe and redirecting char variables (i)
  //fd is used with the open command, basically stores the 

  //Child 1
  if (fork() == 0) {
    //Open the file with read/write commands, 0_CREAT creates the file if it does not exist
    fd = open(file[0], O_RDWR | O_CREAT, 0777);
    dup2(fds[0], 0);

    //Close STDOUT
    close(fds[1]);

    //Read from STDOUT
    while ((count = read(0, &i, 1)) > 0)
    write(fd, &i, 1); // Write to file.

    exit(0);

  //Child 2
  } else if ((pid = fork()) == 0) {
    dup2(fds[1], 1);

    //Close STDIN
    close(fds[0]);

    //Output contents to the given file.
    execvp(cmd[0], cmd);
    perror("execvp failed");

  Parent
  } else {
    waitpid(pid, NULL, 0);
    close(fds[0]);
    close(fds[1]);
  }
}
  • 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-05-17T18:09:10+00:00Added an answer on May 17, 2026 at 6:09 pm

    You’ll need O_APPEND if it’s >> you are looking for.

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

Sidebar

Related Questions

Last time I asked about the reverse process , and got some very efficient
I saw this question asked about C# I would like an answer for PHP.
Another poster asked about preferred syntax for infinite loops . A follow-up question: Why
Another question asked about determining odd/evenness in C, and the idiomatic (x & 1)
I have been asked about creating a site where some users can upload Microsoft
This Question asked about listing directories (not files) in the current directory. I noted
In a related question I asked about binding to a particular element of an
I asked about getting iTextSharp to render a PDF from HTML and a CSS
I asked about Choosing a method to store user profiles the other day and
I asked about finding in subdirs with criteria. First answer was use FindFirstFileEx(). It

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.