欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

1.文件I/O

发布时间:2025/3/8 编程问答 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 1.文件I/O 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一. open()&close()

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h>int main() {int fd;fd = open("abc.txt", O_CREAT|O_RDWR, 0777); // 若flag中使用了O_CREAT,则需要指定第三个参数访问权限if (fd < 0)printf("file create failure");printf("current fd is: %d\n", fd);close(fd);return 0; }

二.read()&write()

write.c

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>static const char *str = "http://www.baidu.com\n";int main() {int fd;fd = open("cde.txt", O_CREAT|O_RDWR|O_APPEND, 0777);write(fd, str, strlen(str));close(fd);return 0; }

read.c

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <unistd.h> #include <stdio.h>int main() {char tmp[30];char str[1024]; int wr_fd;wr_fd = open("aaa.txt", O_CREAT|O_WRONLY|O_APPEND, 0777);int rd_fd;rd_fd = open("cde.txt", O_RDONLY);int total = 0, len;while((len = read(rd_fd, tmp, 30))) {strncpy(str+total, tmp, len);total+=len;}str[total-1] = '\0';close(wr_fd);close(rd_fd);printf("str is: %s\n", str);return 0; }

运行结果:

str is: http://www.baidu.com
http://www.taobao.com
http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com

三.lseek() 移动文件读写指针

使用lseek插入文件内容

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>static const char *str = "http://www.ibm.com\n";int main() {int fd;off_t offset;fd = open("cde.txt", O_RDWR);offset = lseek(fd, 0, SEEK_END);write(fd, str, strlen(str));close(fd); printf("cur offset is: %d\n", offset);return 0; }

运行结果:

http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com
http://www.ibm.com
http://www.ibm.com

使用lseek计算文件大小

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h>int main() {int fd;off_t offset;fd = open("cde.txt", O_RDWR);offset = lseek(fd, 0, SEEK_END);printf("cur file size is: %d\n", offset);close(fd);return 0; }

运行结果:

cur file size is: 208

-rwxrwxr-x 1 yongdaimi yongdaimi 208 Jan 29 00:54 cde.txt

四.fcntl()

使用fcntl()获得文件的flag标志

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h>int main() {int fd;if ((fd = open("hh.txt", O_RDWR | O_CREAT | O_EXCL, 0664)) == -1) {perror("open error");exit(1);} int var;if ((var = fcntl(fd, F_GETFL, 0)) < 0) {perror("fcntl error");close(fd);exit(1);}switch(var & O_ACCMODE) {case O_RDONLY:printf("Read only ...\n");break;case O_WRONLY:printf("Write only ...\n");break;case O_RDWR:printf("Read And Write ...\n");break;default:printf("Do't know...\n");break;}if (var & O_APPEND) {printf("And Append...\n");}if (var & O_NONBLOCK) {printf("And Blocking...\n");}if (close(fd) == -1) {perror("close error");}return 0; }

运行结果:

Read And Write ...

五.ioctl()

使用TIOCGWINSZ命令获得终端设备的窗口大小

#include <sys/ioctl.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <unistd.h>int main() {struct winsize size;if (isatty(STDOUT_FILENO) == 0)exit(1);if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {perror("ioctl TIOCGWINSZ error");exit(1);}printf("%d rows, %d columns\n", size.ws_row, size.ws_col);return 0; }

运行结果:

36 rows, 121 columns

 

总结

以上是生活随笔为你收集整理的1.文件I/O的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。