当前位置:首页>科技>io文件怎么使用文件IO
发布时间:2026-07-21阅读(1)
来源:百问网
作者:韦东山
本文字数:4349,阅读时长:5分钟
参考书:

图1
这 2 本书的内容类似,第一本对知识点有更细致的描述,适合初学者;第二本比较直接,一上来就是各种函数的介绍,适合当作字典,不懂时就去翻看一下。
做纯 Linux 应用的入,看这 2 本书就可以了,不需要学习我们的视频。我们的侧重于“嵌入式 Linux”。
在 Linux 系统中,一切都是“文件”:普通文件、驱动程序、网络通信等等。所有的操作,都是通过“文件 IO”来操作的。所以,很有必要掌握文件操作的常用接口。
1 文件从哪来?
4.2.1通用的 IO 模型:open/read/write/lseek/close
使用 GIT 下载所有源码后,本节源码位于如下目录:
01_all_series_quickstart\04_嵌入式 Linux 应用开发基础知识\source\06_fileio\copy.c
copy.c 源码如下:
0102 #include <sys/types.h>03 #include <sys/stat.h>04 #include <fcntl.h>05 #include <unistd.h>06 #include <stdio.h>0708 /*09 * ./copy 1.txt 2.txt10 * argc = 311 * argv[0] = "./copy"12 * argv[1] = "1.txt"13 * argv[2] = "2.txt"14 */15 int main(int argc, char **argv)16 {17 int fd_old, fd_new;18 char buf[1024];19 int len;2021 /* 1. 判断参数 */22 if (argc != 3)23 {24 printf("Usage: %s <old-file> <new-file>\n", argv[0]);25 return -1;26 }2728 /* 2. 打开老文件 */29 fd_old = open(argv[1], O_RDONLY);30 if (fd_old == -1)31 {32 printf("can not open file %s\n", argv[1]);33 return -1;34 }35淘宝:100ask.taobao.com - 482 - 电话:0755-86200561 官网:www.100ask.net 邮箱:support@100ask.net 100ask 开发板User Manual36 /* 3. 创建新文件 */37 fd_new = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);38 if (fd_new == -1)39 {40 printf("can not creat file %s\n", argv[2]);41 return -1;42 }4344 /* 4. 循环: 读老文件-写新文件 */45 while ((len = read(fd_old, buf, 1024)) > 0)46 {47 if (write(fd_new, buf, len) != len)48 {49 printf("can not write %s\n", argv[2]);50 return -1;51 }52 }5354 /* 5. 关闭文件 */55 close(fd_old);56 close(fd_new);5758 return 0;59 }60
本节源码完全可以在 Ubuntu 上测试,跟在 ARM 板上没什么不同。
执行以下命令编译、运行:
$ gcc -o copy copy.c$ ./copy copy.c new.c
4.2.2不是通用的函数:ioctl/mmap
使用 GIT 下载所有源码后,本节源码位于如下目录:
01_all_series_quickstart\04_嵌入式 Linux 应用开发基础知识\source\06_fileio\copy_mmap.c
在 Linux 中,还可以把一个文件的所有内容映射到内存,然后直接读写内存即可读写文件。
0102 #include <sys/types.h>03 #include <sys/stat.h>04 #include <fcntl.h>05 #include <unistd.h>06 #include <stdio.h>07 #include <sys/mman.h>0809 /*10 * ./copy 1.txt 2.txt11 * argc = 312 * argv[0] = "./copy"13 * argv[1] = "1.txt"14 * argv[2] = "2.txt"15 */16 int main(int argc, char **argv)17 {18 int fd_old, fd_new;19 struct stat stat;20 char *buf;2122 /* 1. 判断参数 */23 if (argc != 3)24 {25 printf("Usage: %s <old-file> <new-file>\n", argv[0]);26 return -1;27 }2829 /* 2. 打开老文件 */30 fd_old = open(argv[1], O_RDONLY);31 if (fd_old == -1)32 {33 printf("can not open file %s\n", argv[1]);34 return -1;35 }3637 /* 3. 确定老文件的大小 */38 if (fstat(fd_old, &stat) == -1)39 {40 printf("can not get stat of file %s\n", argv[1]);41 return -1;42 }43淘宝:100ask.taobao.com - 483 - 电话:0755-86200561 官网:www.100ask.net 邮箱:support@100ask.net 100ask 开发板User Manual44 /* 4. 映射老文件 */45 buf = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd_old, 0);46 if (buf == MAP_FAILED)47 {48 printf("can not mmap file %s\n", argv[1]);49 return -1;50 }5152 /* 5. 创建新文件 */53 fd_new = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);54 if (fd_new == -1)55 {56 printf("can not creat file %s\n", argv[2]);57 return -1;58 }5960 /* 6. 写新文件 */61 if (write(fd_new, buf, stat.st_size) != stat.st_size)62 {63 printf("can not write %s\n", argv[2]);64 return -1;65 }6667 /* 5. 关闭文件 */68 close(fd_old);69 close(fd_new);7071 return 0;72 }73
本节源码完全可以在 Ubuntu 上测试,跟在 ARM 板上没什么不同。
执行以下命令编译、运行:
$ gcc -o copy_mmap copy_mmap.c$ ./copy_mmap copy_mmap.c new2.c
Linux 下有 3 大帮助方法:help、man、info。
想查看某个命令的用法时,比如查看 ls 命令的用法,可以执行:
ls --help
help 只能用于查看某个命令的用法,而 man 手册既可以查看命令的用法,还可以查看函数的详细介绍等等。它含有 9 大分类,如下:
1 Executable programs or shell commands // 命令2 System calls (functions provided by the kernel) // 系统调用,比如 man 2 open3 Library calls (functions within program libraries) // 函数库调用4 Special files (usually found in /dev) // 特殊文件, 比如 man 4 tty 5 File formats and conventions eg /etc/passwd // 文件格式和约定, 比如 man 5 passwd6 Games // 游戏7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) //杂 项8 System administration commands (usually only for root) // 系统管理命令9 Kernel routines [Non standard] // 内核例程
比如想查看 open 函数的用法时,可以直接执行“man open”,发现这不是想要内容时再执行“man 2 open”。
在 man 命令中可以及时按“h”查看帮助信息了解快捷键。常用的快捷键是:
f 往前翻一页b 往后翻一页/patten 往前搜?patten 往后搜
Up Move up one line.Down Move down one line.PgUp Scroll backward one screenful.PgDn Scroll forward one screenful.Home Go to the beginning of this node.End Go to the end of this node.TAB Skip to the next hypertext link.RET Follow the hypertext link under the cursor.l Go back to the last node seen in this window.[ Go to the previous node in the document.] Go to the next node in the document.p Go to the previous node on this level.n Go to the next node on this level.u Go up one level.t Go to the top node of this document.d Go to the main directory node.


「新品首发」STM32MP157开发板火爆预售!首批仅300套
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图