pipe

This is a discussion on pipe within the C Programming forums, part of the General Programming Boards category; i have written the following program. The function of this prog is to read data from a file(source.c) and write ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    pipe

    i have written the following program. The function of this prog is to read data from a file(source.c) and write into another file(dest.c) using pipes. I have just written a line in the source file.Im able to compile and run the program without errors. But the data is not written onto the other file(dest.c);
    please help!!!!!!!!!!!!!!!!!!!!!!

    Code:
    #include <sys/wait.h>
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    int main()
      {
          int pfd[2],i=0,j=0;;
          pid_t cpid;
          char buf[20],ch,c;
          FILE *fp1,*fp2;
    
          fp1=fopen("source.c","r+");
          if(fp1==NULL)
    	{
    	    perror("Error opening source file\n");
    	}
          while((ch=getc(fp1))!=EOF)
    	{
    	    buf[i]=ch; 
    	    printf("i=%d ch=%c\n",i,ch);
    	    i++;
    	}
    	buf[i] = '\0';
          fclose(fp1);
    
          if (pipe(pfd) == -1) 
    	{
    	    perror("pipe");
    	    exit(EXIT_FAILURE);
    	}
    
          cpid = fork();
          if (cpid == -1) 
    	{
    	    perror("fork");
    	    exit(EXIT_FAILURE);
    	}
    
          if (cpid == 0) 
    	{    /* Child reads from pipe */
                char arr;
    	    close(pfd[1]);          /* Close unused write end */
          fp2=fopen("dest.c","w+");              
          if(fp2==NULL)
    	{
    	    perror("Error opening dest file\n");
    	    _exit(1);
    	} 
    	    printf("in client\n");
    	    int i = 0;
    	    while (read(pfd[0], &arr, 1) > 0)
    	      {
    	      	  i++;
    	      	  printf("\n %c", arr);
    		  putc((int)arr, fp2);
    	      }
    	      //read(pfd[0], &arr, 1);
    	      printf("\n %c", arr);
    	      printf("i = %d\n", i);
    	    close(pfd[0]);
    	    _exit(EXIT_SUCCESS);
    
    	} 
          else 
    	{            /* Parent writes argv[1] to pipe */
    	    j=0;
    	    close(pfd[0]);          /* Close unused read end */
    		  write(pfd[1], buf, strlen(buf));
    		  j++;
    	    close(pfd[1]);          /* Reader will see EOF */
    	    wait(NULL);             /* Wait for child */
    	    exit(EXIT_SUCCESS);
    	}
          fclose(fp2);
      }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    I imagine the immediate problem is that, in your child, you aren't closing fp2. Since you use _exit() instead of exit() (which does make sense in this context), output streams won't necessarily be flushed, and that's probably the case here. You'll also not want to have the fclose(fp2) at the end of your program. While it turns out that that statement will never be reached (all branches exit), and so it won't trigger a runtime problem, it still is out of place.

    A couple other things I noticed: ch should be an int, not a char. This may be slightly confusing, because you're using it to store a character, but there is a good reason to make it int (explanation can be provided if you're interested). In addition, there's no need to cast your char to an int when calling putc(). Please, please get in the habit of not casting. Generally speaking, a cast is the wrong thing to do. They should only be used in a few circumstances, and this is not one of them.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    ya i got it... thanx... i will consider your advice of not to use casting..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  2. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  3. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  4. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM
  5. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21