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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:11:05+00:00 2026-06-01T21:11:05+00:00

I need to write a program that create pipe send filename from command line

  • 0

I need to write a program that create pipe send filename from command line to child process. In child read that file and send it back using pipe. Parent process should print the file. if error occur in child process error must be send to parent process.

here is my code, it print some junk along file file (and also it disable scrolling in terminal emulator when I run it).

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void main(int argc, char *argv[]) {
   int pipefd[2];
   char buff[100];
   int childpid;
   int size;
   FILE *file;

   if (argc != 2) {
      printf("usage:\n%s <filename>\n", argv[0]);
      exit(1);
   }
   if (pipe(pipefd) < 0) {
       perror("can't open pipe\n");
   }
   if ((childpid = fork()) == 0) {
      sleep(1);
      size = read(pipefd[0], buff, sizeof(buff));
      file = fopen(buff, "r");
      if (file == NULL) {
         write(pipefd[1], "Can't open file", 15);
         exit(1);
      }
      while (!feof(file)) {
         if (fgets(buff, sizeof(buff), file) == NULL) {
            write(pipefd[1], "Error reading file", 18);
         } else {
            write(pipefd[1], buff, sizeof(buff));
         }
      }
   } else if (childpid > 0) {
      size = strlen(argv[1]);
      if (write(pipefd[1], argv[1], size) != size) {
         perror("Error writing to pipe\n");
      }
      wait(NULL);
      while ((size = read(pipefd[0], buff, sizeof(buff))) > 0) {
         write(1, buff, size);
      }
   }
   exit(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-01T21:11:06+00:00Added an answer on June 1, 2026 at 9:11 pm

    Your program works as intended after quite a few changes. Lets list out what all changes are required and why-

    I) Both in the child and parent, close the respective pipes as soon as you are done with them. From man page of read(3),

    If some process has the pipe open for writing and O_NONBLOCK is clear,
    read() shall block the calling thread until some data is written or
    the pipe is closed by all processes that had the pipe open for
    writing.

    So do something like this in your code everywhere where the job pipes is over,

      size = read(pipefd[0], buff, sizeof(buff));
      close(pipefd[0]);
    
      write(pipefd[1], buff, strlen(buff));
      close(pipefd[1]);
    
      if (write(pipefd[1], argv[1], size) != size) {
         perror("Error writing to pipe\n");
      }
      close(pipefd[1]);
    
      while ((size = read(pipefd[0], buff, sizeof(buff))) > 0) 
      {
         write(1, buff, size);
      }
      close(pipefd[0]);
    

    You hadn’t closed the write end of of the pipe in the child and your parent was blocking in the read

    II) You are using something like while(fgets(...)) in a loop to read data from file. This will bomb when there are newlines in the file and fgets returns multiple times, overwriting the buffer everytime during the process

    I always use simple fgetc and feof combination to read from file. So, change your file reading mechanism to something like

    unsigned count=0;
    while (!feof(file) && count < sizeof(buff))
        buff[count++]=fgetc(file);
    if (feof(file)) 
        buff[--count]=0;
    else
        buff[sizeof(buff)-1]=0;
    

    III) While writing the file data from the child, you should use strlen(as we have already made sure buffer is null terminated, see above ) and not sizeof as the buffer may not be full at all and you will end up writing junk. So, change

      write(pipefd[1], buff, sizeof(buff));
    

    to

      write(pipefd[1], buff, strlen(buff));
    

    IV) Follow a safe exit from the child and parent after their job is done. Something like

    close(pipefd[1]);
    _exit(EXIT_SUCCESS);   // in child
    

    and

    close(pipefd[0]);
    exit(EXIT_SUCCESS); // in parent
    

    PS: I’ve changed the file reading logic, so your compiler error is gone now and do follow the advice given by n.m.

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

Sidebar

Related Questions

I need to write a haskell program that retrieves a file from command line
I need to write a simple program that records all the input from parallel
I need to write a simple program for work that does the following: read
I need to create a virtual file, a file that if some program access
I need to write a program that uses matrix multiplication to rotate an image
I'm going to need to write a program that takes a list of persons
I need to write a little program in C that parses a string. I
I have a program that looks like this. I need to consistently write something
I have a robot that I need to write an autonomous program for. The
I need to write a Perl script that pipes input into a Java program.

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.