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

A co-worker asked about some code like this that originally had templates in it.
I recently asked about accessing data from SPSS and got some absolutely wonderful help
The previous question I asked about creating a DIV where size is calculated from
Last time I asked about the reverse process , and got some very efficient
Some one asked about How to connect to SQL server database from JavaScript? and
Yesterday I asked about serving byte ranges from PHP. Today my question is -
A similar question was asked about a year ago, and wasn't quite resolved, but
Ok, I have asked a few questions about different facets of trying to accomplish
Another question asked about the meaning of the code snippet a >>> 0 in
I've asked about this earlier but the question itself and all the information in

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.