文件操作
来源:互联网 发布:图标素材 知乎 编辑:程序博客网 时间:2024/05/16 15:45
文件操作 有关系统调用 写文件的相关程序
write.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define SIZE 1024
int main()
{
int fd = open("abc", O_WRONLY|O_CREAT, 0777);
if (fd == -1)
{
perror ("open");
return -1;
}
char buf[SIZE] = {0};
while (1)
{
fgets (buf, SIZE, stdin);
if (strncmp ("end", buf, 3) == 0)
break;
ssize_t ret = write(fd, buf, strlen(buf));
if (ret == -1)
{
perror ("write");
}
printf ("要写的字节数 :%d, 实际写的字节数: %d\n", SIZE, ret);
}
close(fd);
return 0;
}