#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define N 128
int main(int argc, const char *argv[])
{
int fd;
char buf[N] = { 0 };
memset(buf, 0, sizeof(buf));
if ((fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
{
perror("open");
return -1;
}
while (fgets(buf, 20, stdin) != NULL)
{
if (strcmp(buf, "quit\n") == 0)
break;
write(fd, buf, strlen(buf));
}
close(fd);
return 0;
}
Linux下C中使用write函数实现将键盘输入的内容写入文件,直到输入quit退出程序
最新推荐文章于 2023-02-10 17:26:38 发布