grassboy 0 Newbie Poster

Hello...
I've studied FIFO in recent days...
and tried to write some code (Linux C) for practice...

but there are some bugs in my code...
I don't know how to solve it...

here is my code...

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
        char path[200]="./fifo.tmp";
        char str[200];
        int i, op;
        int myFifo;
        scanf("%d", &op);
        if(op %2 ==0){
                if(mkfifo(path, S_IRWXU | S_IRWXO | S_IRWXG)==-1)
                {
                        fprintf(stderr, "fail fifo\n"); fflush(stderr);
                }
                myFifo = open(path, O_RDWR);
                close(1);
                dup(myFifo);
                printf("yoyoyo");fflush(stdout);

                scanf("%d", &i);

        } else {
                myFifo = open(path, O_RDWR);

                //version 1
                read(myFifo, str, 6);

                //version 2 (with dup fifo to stdin)
                close(0);
                dup(myFifo);
                scanf("%s", str);

                fprintf(stderr, "---%s\n",str); fflush(stderr);

        }
}

after I build my program...

first, I run my program and type "0" to create a FIFO
and write some characters to FIFO
my program will stopped at scanf("%d", &i);

at this time, I run the same program, but type "1" to try to read the content of FIFO
If I use the code of version 1
the can be read by read(myFifo, str, 6);
but if I use the code of version 2
it can't be read by scanf("%s", str);
althought the FIFO has been dup to stdin

did I have some misconception about mkfifo() function ?
and how to fix my code ?

thanks.