下面的代码应该写入“一些文本”,以demo.txt,但它不工作:fdopen和fprintf
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
FILE *fp;
int fd;
if ((fd = open("demo.txt", O_RDWR)) == -1) {
perror("open");
exit(1);
}
fp = fdopen(fd, "w");
fprintf(fp, "some text\n");
close(fd);
return 0;
}
你确定是否存在demo.txt文件? – Whoami
是的,当然,和dprintf(fd,“一些文本\ n”)工作 –