The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 08-24-2008
tolkki tolkki is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 7
thanks it helped a lot!!
Actually,I have another prob right now...
i am trying to make the command cat <filename> | wc -l

well,there seems to be a problem in the reading of the last line from the buffer

Code:
//implementation of cat <filename> | wc -l

#include <stdio.h>
#include <string.h>
#define size 500
int main(int argc,char * argv[])
{

   int pipe1[2];
   int childpid;
   FILE * fp;
   char buffer[size];
   int status;
   int lines;
   if(argc!=2)
   {
     printf("usage : mycat <file name>\n");
     exit(1);
   }
   pipe(pipe1);
   childpid=fork();
   if(childpid>0)
   {
      close(pipe1[0]);
      fp=fopen(argv[1],"r");
      if(fp==NULL)
      {
         printf("Cannot open file");
         exit(1);
      }
     while(fgets(buffer,sizeof(buffer),fp)!=NULL)
       write(pipe1[1],buffer,strlen(buffer));

      fclose(fp);
      wait(&status);
      close(pipe1[1]);
      exit(0);
   }
   else
   {
      close(pipe1[1]);
      lines=0;
       while (read(pipe1[0],buffer,sizeof(buffer)) > 0)
           lines++;
      printf("%d",lines);
      close(pipe1[0]);
      exit(0);
   }
}
take a look if you can...thx in advance...