五、文件IO函数

it2026-08-28  3

对于传统的操作系统来说,普通的 I/O 操作一般会被内核缓存,这种 I/O 被称作缓存I/O。本文所介绍的文件访问机制不经过操作系统内核的缓存,数据直接在磁盘和应用程序地址空间进行传输,所以该文件访问的机制称作为直接 I/O。Linux 中就提供了这样一种文件访问机制,对于那种将 I/O 缓存存放在用户地址空间的应用程序来说,直接 I/O 是一种非常高效的手段。

一、打开文件函数open

int open(const char *path, int oflags);int open(const char *path, int oflags,mode_t mode);

     – 参数path表示:路径名或者文件名。路径名为绝对路径名。      – 参数oflags表示:打开文件所采取的动作 O_RDONLY文件只读;O_WRONLY文件只写;O_RDWR文件可读可写;         O_NOCTTY如果路径指向终端,则不将设备作为此进程的控制终端 ;O_NDELAY非阻塞方式操作文件。      – mode表示:设置创建文件的权限。权限的宏定义很麻烦,可以直接用数字(777)替代。      – 返回值:出错返回-1;否则返回文件句柄。

我们在home/linuxsystemcode/io_file目录下创建open.c文件,具体代码如下:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> main(){ int fd; char *leds = "/dev/leds"; // 存在的文件 char *test1 = "/bin/test1"; // 不存在的文件 char *test2 = "/bin/test2"; // 不存在的文件,我们以O_CREAT的形式打开 if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0){ printf("open %s failed!\n",leds); } printf("\n%s fd is %d\n",leds,fd); if((fd = open(test1,O_RDWR,0777))<0){ printf("open %s failed!\n",test1); } printf("%s fd is %d\n",test1,fd); if((fd = open(test2,O_RDWR|O_CREAT,0777))<0){ printf("open %s failed!\n",test2); } printf("%s fd is %d\n",test2,fd); }

 然后使用GCC编译器编译:

最后拷贝出open文件,放到U盘,然后运行,具体如下图: 

[ 5636.146944] LEDS_CTL DEBUG:Device Opened Success! [ 5636.151300] LEDS_CTL DEBUG:Device Closed Success!            为内核提示的打开和关闭文件成功。

 /dev/leds fd is 3              leds的文件句柄是3 open /bin/test1 failed!      test1打开失败 /bin/test1 fd is -1             test1失败,返回-1; /bin/test2 fd is 4              test2创建并打开成功,文件句柄为4;

二、创建文件函数create和open

int creat(const char * pathname, mode_t mode);//注意,这个没写错,就是creat

         – 参数path表示:路径名或者文件名。路径名为绝对路径名。          – 参数oflags表示:打开文件所采取的动作: O_RDONLY文件只读;O_WRONLY文件只写;O_RDWR文件可读可写

我们在home/linuxsystemcode/io_file目录下创建creat.c文件,具体代码如下: 

//标准输入输出头文件 #include <stdio.h> //文件操作函数头文件 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> main() { int fd; //开发板中已经存在/dev/leds文件 char *leds = "/dev/leds"; //开发板中不存在的文件/bin/test1 char *test1 = "/bin/test1"; //开发板中不存在的文件/bin/test2 char *test2 = "/bin/test2"; //需要新建的文件/bin/test3 char *test3 = "/bin/test3"; //使用open函数打开文件 if((fd = open(leds, O_RDWR|O_NOCTTY|O_NDELAY))<0){ printf("open %s failed\n",leds); } printf("%s fd is %d\n",leds,fd); //使用open函数打开不存在的文件,不添加O_CREAT标识符,会报错 if((fd = open(test1, O_RDWR))<0){ printf("open %s failed\n",test1); } //打开文件创建文件,添加标志位O_CREAT表示不存在这个文件则创建文件 if((fd = open(test2, O_RDWR|O_CREAT,0777))<0){ printf("open %s failed\n",test2); } printf("%s fd is %d\n",test2,fd); fd = creat(test3,0777); if(fd = -1){ printf("%s fd is %d\n",test3,fd); } else{ printf("create %s is succeed\n",test3); } }

和上面openy一样编译,然后靠到U盘里,插入开发板运行:

 

三、写函数write

ssize_t  write(int fd, const void *buf, size_t count);

         – 参数fd表示:使用open 函数打开文件之后返回的句柄。          – 参数*buf表示:写入的数据          – 参数count表示:最多写入字节数          – 返回值:出错-1,;其它数值表示实际写入的字节数

具体程序如下:

//标准输入输出头文件 #include <stdio.h> //文件操作函数头文件 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> main() { int fd; char *testwrite = "/bin/testwrite"; ssize_t length_w; char buffer_write[] = "Hello Write Function!"; if((fd = open(testwrite, O_RDWR|O_CREAT,0777))<0){ printf("open %s failed\n",testwrite); } //将buffer写入fd文件 length_w = write(fd,buffer_write,strlen(buffer_write)); if(length_w == -1) { perror("write"); } else{ printf("Write Function OK!\n"); } close(fd); }

运行效果如下: 

 

四、文件的读取函数read

ssize_t  read(int fd,void *buf,size_t len);

       – 参数fd:使用open 函数打开文件之后返回的句柄        – 参数*buf:读出的数据保存的位置        – 参数len:每次最多读len 个字节        – 返回值:错误返回-1,执行成功返回实际读取值

具体程序如下:

//标准输入输出头文件 #include <stdio.h> //文件操作函数头文件 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #define MAX_SIZE 1000 main(){ int fd; ssize_t length_w,length_r = MAX_SIZE,ret; char *testwrite = "/bin/testwrite"; char buffer_write[] = "Hello Write Function!"; char buffer_read[MAX_SIZE]; if((fd = open(testwrite,O_RDWR|O_CREAT,0777))<0){ printf("open %s failed!\n",testwrite); } length_w = write(fd,buffer_write,strlen(buffer_write)); if(length_w == -1){ perror("write"); } else{ printf("Write Function OK!\n"); } close(fd); if((fd = open(testwrite,O_RDWR|O_CREAT,0777))<0){ printf("open %s failed!\n",testwrite); } if(ret = read(fd,buffer_read,length_r)){ perror("read"); } printf("Files Content is %s \n",buffer_read); close(fd); }

 运行效果如下:

五、关闭函数close

int close(int fd);

       – 参数fd:使用open 函数打开文件之后返回的句柄。

参考上面write程序最后一行。

最后,我把我们实验过程的源码和生成的文件打包一起,需要可以下载(没积分留言,我发给你):

 

 

最新回复(0)