我正在嘗試編寫2個程序,這兩個程序將使用fifo管道相互交談。 我使用的例子here(5.2節),但我改變了mknod那裏mkfifo並試圖改變獲取fgets。 這是代碼(其中寫入到FIFO一個程序):mkfifo使2個進程互相交談
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h> /*mkfifo, open */
#include <sys/wait.h>
#include <sys/stat.h> /* mkfifo, open */
#include <fcntl.h> /*open */
#define FIFO_PATH "/home/hana/Desktop"
#define BUFFER_SIZE 300
int main()
{
char buffer[BUFFER_SIZE];
int fd;
int wStatus;
mkfifo(FIFO_PATH, 666);
printf("waiting for readers\n");
fd = open(FIFO_PATH, O_RDWR);
while (fgets(buffer, BUFFER_SIZE, fd), !feof(stdin))
{
if ((wStatus = write(fd, buffer, strlen(buffer))) == -1)
perror("write");
else
printf("speak: wrote %d bytes\n", wStatus);
}
return 0;
}
我得到一個編譯錯誤:使與fgets的參數3使指針從整數。 所以fgets期待FILE *而不是文件描述符。 我該怎麼辦?改變一些東西,以便fgets工作?使用另一個功能?
我正在編譯與gcc(ansi,迂腐)。
感謝
由於OP只詢問編譯錯誤以及如何獲取'FILE *',所以我沒有真正閱讀代碼。 :) – pynexj
@whjm我不是暗示你的回答是錯誤的。事實上你的投票是我的。 – JeremyP
我還更改了主題並刪除了標籤'ipc'和'mkinfo'。剛剛回滾。 :) – pynexj