Thread: pipe

  1. #16
    Registered User
    Join Date
    Mar 2013
    Posts
    20
    Hey guys, I had to make some changes and how come this always returns 0 characters if I do like this in the parent:

    [C] #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ - Pastebin.com

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Please post the code on this site, not some link.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Mar 2013
    Posts
    20
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    
    
    main(int argc, char* argv[])
    {
      int fds[2], pid, i, fd;
      char *pipeBuffer;
      fd = open(argv[1], O_RDWR | O_APPEND);
    
      if(argc != 2)
      {
        printf("Usage: my_program <filname of textfile>\n");
        exit(0);
      }
    
      //create a pipe
      if(pipe(fds) == -1)
      {
        perror("pipe");
        exit(EXIT_FAILURE);
      }
    
      //create a fork
      if((pid = fork()) == -1) 
      {
        perror("fork");
        exit(EXIT_FAILURE);
      }
    
    
      if(pid == 0) 
      {
        close(fds[1]); 
    
    
        int letterCounter = 0;
        int counter = 0;
        printf("\n");
    
    
        //read letters from the pipe
        while(read(fds[0], &pipeBuffer[counter], 1) > 0)
        {
          if(isalpha(pipeBuffer[counter]))
          {
            letterCounter++;
          }
    
    
          counter++;
        }
    
    
        //write number of letters to end of file
        char tmp[1]={0x0};
        sprintf(tmp,"%d", letterCounter);
        write(fd, tmp, strlen(tmp));
    
    
        printf("Letters: %d\n\n", letterCounter);
    
    
        close(fd);
        close(fds[0]);
        _exit(EXIT_SUCCESS);
      }
      else 
      {
        close(fds[0]); 
    
    
        int c;
        FILE *fdx, *fdy;
        fdx = fdopen(fd, "r");
        fdy = fdopen(fds[1], "w");
    
    
        while((c = fgetc(fdx)) != EOF)
        {
          printf("%c", c);
          fputc(c, fdy);
        }
    
    
        printf("\n\n");
    
    
        fclose(fdx);
        fclose(fdy);
    
    
        close(fds[1]); 
        wait(NULL);
        exit(EXIT_SUCCESS);
      }
    }
    Last edited by Gatsu; 03-25-2013 at 01:57 AM.

  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Compiling with warnings I get:
    Code:
    $ make foo
    cc -Wall -Wextra -ggdb3    foo.c   -o foo
    foo.c:11:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    foo.c: In function ‘main’:
    foo.c:51:7: warning: implicit declaration of function ‘isalpha’ [-Wimplicit-function-declaration]
    foo.c:13:20: warning: unused variable ‘i’ [-Wunused-variable]
    foo.c:51:28: warning: ‘pipeBuffer’ may be used uninitialized in this function [-Wuninitialized]
    Code:
    char *pipeBuffer;
    ...
    while(read(fds[0], &pipeBuffer[counter], 1) > 0)
    "pipeBuffer" points to anywhere. And indexing it will likely result in a crash with a bigger file.
    But since you just want to read one character after another you don't need to use a buffer. Just do
    Code:
    char c;
    read(fds[0], &c, 1);

    Here's another buffer overrun:
    Code:
    char tmp[1]={0x0};
    sprintf(tmp,"%d", letterCounter);
    How many characters can you store in "tmp"?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe multiple programs using pipe() and c
    By HaitiBoy in forum C Programming
    Replies: 1
    Last Post: 12-07-2010, 05:19 PM
  2. pipe
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 09-23-2008, 01:35 AM
  3. Pipe
    By quickNitin in forum C Programming
    Replies: 4
    Last Post: 09-06-2006, 03:04 AM
  4. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  5. pipe()
    By bojan in forum C Programming
    Replies: 2
    Last Post: 04-13-2006, 07:47 AM