reading from a file and writing to other using pipe - Unix

This is a discussion on reading from a file and writing to other using pipe - Unix ; This is a program which I wrote. Please help me identifying the error or suggest the alternative solution. #include #include #include int main() { FILE *fp1, *fp2; int fd[2],i; char ch; fp1 = fopen("input", "r"); fp2 = fopen("output","w"); if(pipe(fd) exit(1); ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: reading from a file and writing to other using pipe

  1. reading from a file and writing to other using pipe

    This is a program which I wrote. Please help me identifying the error
    or suggest the alternative solution.

    #include
    #include
    #include

    int main()
    {
    FILE *fp1, *fp2;
    int fd[2],i;
    char ch;
    fp1 = fopen("input", "r");
    fp2 = fopen("output","w");

    if(pipe(fd)<0)
    exit(1);

    //while(1)
    for(i =0; i<54; i++)
    {
    if(fork()==0)
    {
    close(fd[0]);
    if((ch=fgetc(fp1))==EOF) break;
    write(fd[1], &ch, 1 );
    }
    else
    {
    close(fd[1]);
    read(fd[0], &ch, 1 );
    fputc(ch, fp2);
    }
    }

    fclose(fp1);
    fclose(fp2);


    }

  2. Re: reading from a file and writing to other using pipe

    On Aug 21, 10:44*am, Shashank wrote:
    > This is a program which I wrote. Please help me identifying the error
    > or suggest the alternative solution.
    >
    > #include
    > #include
    > #include
    >
    > int main()
    > {
    > * * * * FILE *fp1, *fp2;
    > * * * * int fd[2],i;
    > * * * * char ch;
    > * * * * fp1 = fopen("input", "r");
    > * * * * fp2 = fopen("output","w");
    >
    > * * * * if(pipe(fd)<0)
    > * * * * * * * * exit(1);
    >
    > * * * * //while(1)
    > * * * * for(i =0; i<54; i++)
    > * * * * {
    > * * * * if(fork()==0)


    Why are you forking 54 times?

    > * * * * {
    > * * * * * * close(fd[0]);
    > * * * * * * if((ch=fgetc(fp1))==EOF) break;
    > * * * * * * write(fd[1], &ch, 1 );
    > * * * * }
    > * * * * else
    > * * * * {
    > * * * * * * close(fd[1]);
    > * * * * * * read(fd[0], &ch, 1 );
    > * * * * * * fputc(ch, fp2);
    > * * * * }
    > * * }
    >
    > * * fclose(fp1);
    > * * fclose(fp2);
    >
    >
    >
    > }
    >


    What are you trying to do? What did you expect this program to do?
    What does it actually do? What is the "error" you say is happening?
    --
    Fred Kleinschmidt

  3. Re: reading from a file and writing to other using pipe

    On 21 Aug, 18:44, Shashank wrote:
    > This is a program which I wrote. Please help me identifying the error
    > or suggest the alternative solution.
    >
    > #include
    > #include
    > #include
    >
    > int main()
    > {
    > * * * * FILE *fp1, *fp2;
    > * * * * int fd[2],i;
    > * * * * char ch;
    > * * * * fp1 = fopen("input", "r");
    > * * * * fp2 = fopen("output","w");
    >
    > * * * * if(pipe(fd)<0)
    > * * * * * * * * exit(1);
    >
    > * * * * //while(1)
    > * * * * for(i =0; i<54; i++)
    > * * * * {
    > * * * * if(fork()==0)
    > * * * * {
    > * * * * * * close(fd[0]);
    > * * * * * * if((ch=fgetc(fp1))==EOF) break;
    > * * * * * * write(fd[1], &ch, 1 );
    > * * * * }
    > * * * * else
    > * * * * {
    > * * * * * * close(fd[1]);
    > * * * * * * read(fd[0], &ch, 1 );
    > * * * * * * fputc(ch, fp2);
    > * * * * }
    > * * }
    >
    > * * fclose(fp1);
    > * * fclose(fp2);
    >
    > }


    First time through the loop, the parent closes fd[ 0 ]. The second
    time in the loop, the new child is attempting to read from
    that closed pipe.

    What exactly are you trying to do?

  4. Re: reading from a file and writing to other using pipe

    On Aug 21, 11:17*am, Fred wrote:

    > Why are you forking 54 times?


    He's forking a lot more than 54 times! It would take the known age of
    the universe for this program to complete all the forking it tries to
    do.

    DS

  5. Re: reading from a file and writing to other using pipe

    William Pursell writes:
    >On 21 Aug, 18:44, Shashank wrote:
    >> This is a program which I wrote. Please help me identifying the error
    >> or suggest the alternative solution.
    >>
    >> #include
    >> #include
    >> #include
    >>
    >> int main()
    >> {
    >> =A0 =A0 =A0 =A0 FILE *fp1, *fp2;
    >> =A0 =A0 =A0 =A0 int fd[2],i;
    >> =A0 =A0 =A0 =A0 char ch;
    >> =A0 =A0 =A0 =A0 fp1 =3D fopen("input", "r");
    >> =A0 =A0 =A0 =A0 fp2 =3D fopen("output","w");
    >>
    >> =A0 =A0 =A0 =A0 if(pipe(fd)<0)
    >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 exit(1);
    >>
    >> =A0 =A0 =A0 =A0 //while(1)
    >> =A0 =A0 =A0 =A0 for(i =3D0; i<54; i++)
    >> =A0 =A0 =A0 =A0 {
    >> =A0 =A0 =A0 =A0 if(fork()=3D=3D0)
    >> =A0 =A0 =A0 =A0 {
    >> =A0 =A0 =A0 =A0 =A0 =A0 close(fd[0]);
    >> =A0 =A0 =A0 =A0 =A0 =A0 if((ch=3Dfgetc(fp1))=3D=3DEOF) break;
    >> =A0 =A0 =A0 =A0 =A0 =A0 write(fd[1], &ch, 1 );
    >> =A0 =A0 =A0 =A0 }
    >> =A0 =A0 =A0 =A0 else
    >> =A0 =A0 =A0 =A0 {
    >> =A0 =A0 =A0 =A0 =A0 =A0 close(fd[1]);
    >> =A0 =A0 =A0 =A0 =A0 =A0 read(fd[0], &ch, 1 );
    >> =A0 =A0 =A0 =A0 =A0 =A0 fputc(ch, fp2);
    >> =A0 =A0 =A0 =A0 }
    >> =A0 =A0 }
    >>
    >> =A0 =A0 fclose(fp1);
    >> =A0 =A0 fclose(fp2);
    >>
    >> }

    >
    >First time through the loop, the parent closes fd[ 0 ]. The second
    >time in the loop, the new child is attempting to read from
    >that closed pipe.
    >
    >What exactly are you trying to do?


    Not to mention the fork in (originally) a 'while(1)' loop. Can you say fork bomb?

    s

+ Reply to Thread