文件操作

xiaoxiao2021-02-28  86

文件操作  有关系统调用  写文件的相关程序

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; }

转载请注明原文地址: https://www.6miu.com/read-70494.html

最新回复(0)