I want to create a named pipe and then write to it and after that I want read it. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#define FIFO "fifo0001"
int main(intargc, char *argv[]){
char input[256];
FILE *fp;
char str[50];
printf("Please write some text:\n");
scanf("%s", input);
unlink(FIFO); /* Because it already exists, unlink it before */
umask(0);
if(mkfifo(FIFO, 0666) == -1){
printf("Something went wrong");
return EXIT_FAILURE;
}
if((fp = fopen(FIFO, "a")) == NULL){
printf("Something went wrong");
return EXIT_FAILURE;
}
fprintf(fp, "%s", input);
if(fgets(str, 50, fp) != NULL){
puts(str);
}
fclose(fp);
return EXIT_SUCCESS;
}
After I write a text, nothing happen anymore. And there is no message. I have to quit the program with STRG C. Someone know what is wrong? I have to use the functions mkfifo, fopen, fprintf, fgets and fclose. So would be nice if I could keep them into the code.