Reputation: 185
I have to use mkfifo
in my C program in Ubuntu. But I have an error when I run the code: no such file or directory
.
I think the problem because I have not set the panel_fifo
environment variables. But I don't know how could I do this.
Here is my code I use to test this method:
char *myfifo="./sock/myfifo";
if (mkfifo(myfifo,0777)<0)
perror("can't make it");
if (fd=open(myfifo,O_WRONLY)<0)
perror("can't open it");
I compile this with:
gcc gh.c -o gh
When I run, I get this error message:
can't make it:no such file or directory
can't open it:no such file or directory
Upvotes: 1
Views: 722
Reputation: 754920
See How can I create a directory tree in C++/Linux for a general C (and C++) solution to creating a directory path. For the immediate problem, that is overkill and a direct call to mkdir()
suffices.
const char dir[] = "./sock";
const char fifo[] = "./sock/myfifo";
int fd;
if (mkdir(dir, 0755) == -1 && errno != EEXIST)
perror("Failed to create directory: ");
else if (mkfifo(fifo, 0600) == -1 && errno != EEXIST)
perror("Failed to create fifo: ");
else if ((fd = open(fifo, O_WRONLY)) < 0)
perror("Failed to open fifo for writing: ");
else
{
…use opened fifo…
close(fd);
}
I'm assuming you have the correct headers included, of course (<errno.h>
, <fcntl.h>
, <stdio.h>
, <stdlib.h>
, <sys/stat.h>
, <unistd.h>
, I believe).
Note the parentheses around the assignment in the if
that opens the FIFO.
Upvotes: 3