文件IO

在 Linux 系统中,一切都是“ 文件”:普通文件、驱动程序、网络通信等等。所有的操作,都是通过“文件 IO”来进行的。所以,很有必要掌握文件操作的常用接口。

Linux系统的文件有哪些

Linux 的文件既可以是真实保存到存储介质的文件也可以是自身内核提供的虚拟文件,还可以是设备节点 。

访问文件的方式
类型 方法
通用的 IO 模型: open/read/write/lseek/close
非通用的函数 ioctl/mmap
Linux下的帮助方法
方法 功能
xxx --help 单个命令的用法
man 分类号 xxx 用法与函数详细介绍(最常用)
info 更加详细的内容(不常用)

man的9大分类:

1 Executable programs or shell commands // 命令
2 System calls (functions provided by the kernel) // 系统调用,比如 man 2 open
3 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 passwd
6 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] // 内核例程
系统调用怎么进入内核以及内核的 sys_open、 sys_read 会做什么

详见《完全开发手册》P171-P172

文件IO的常用函数(可通过man方法获取更多细节)
函数 功能
int open(const char *pathname, int flags, mode_t mode); 建立一条到文件或设备的访问路径,返回文件描述符。mode参数只有使用 O_CREAT 标志创建一个新文件时才有效。
ssize_t read(int fd, void *buf, size_t count); 通过文件描述符读字节到缓冲区(物理内存),并返回字节数,若文件为空,则返回-1
ssize_t write(int fd, const void *buf, size_t count); 通过文件描述符,从buf开始写count个字节到文件
int fstat(int fd, struct stat *statbuf); 返回文件的状态信息到statbuf结构体,通过结构体存储文件状态
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); 将磁盘文件映射到内存(虚拟内存),实际上会返回内存映射的起始地址

标准IO方式:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h> /**
* argv[1]:新文件
* argv[2]:旧文件
**/
int main(int argc, char **argv)
{
int fd_old, fd_new;
char data[1024]; //1024个字节为一组
int len;
/*格式提醒*/
if(argc != 3) {
printf("Usage: %s <old-file> <new-file>\n", argv[0]);
return -1;
}
/* 1.打开文件 */
fd_old = open(argv[2], O_RDONLY);
if(fd_old == -1) {
printf("can not open file %s\n", argv[2]); //打开文件失败
return -1;
}
/* 2.创建新文件 */
fd_new = open(argv[1], O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(fd_new == -1) {
printf("can not creat file %s\n", argv[1]);
return -1;
}
/* 3.读取旧文件,写入新文件 */
while((len = read(fd_old, data, 1024)) > 0) {
if(write(fd_new, data, len) != len) {
printf("can not wite file %s\n", argv[2]);
return -1;
}
}
/* 4.关闭文件 */
close(fd_old);
close(fd_new); return 0;
}

非通用IO(mmap):


#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h> int main(int argc, char **argv) {
int fd_old, fd_new;
struct stat stat;
char *ptr; //内存映射的起始地址
/* 1.判断指令 */
if(argc != 3) {
printf("Usage: %s <new-file> <old-file>\n", argv[0]);
return -1;
} /* 2.打开旧文件 */
fd_old = open(argv[2], O_RDONLY);
if(fd_old == -1) {
printf("can not open %s\n", argv[2]);
return -1;
} /* 3.获取文件长度 */
if(fstat(fd_old, &stat) == -1) { //获取文件信息
printf("can not get stat of %s\n", argv[2]);
return -1;
} /* 4.映射旧文件 */
ptr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd_old, 0);
if(ptr == MAP_FAILED) {
printf("can not mmap file %s\n", argv[2]);
return -1;
} /* 5.创建新文件 */
fd_new = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(fd_new == -1) {
printf("can not creat file %s\n", argv[2]);
return -1;
} /* 6.写新文件 */
if(write(fd_new, ptr, stat.st_size) != stat.st_size) {
printf("can not write %s\n", argv[1]);
return -1;
} /* 7.关闭文件 */
close(fd_new);
close(fd_old); return 0;
}

嵌入式Linux—文件IO的更多相关文章

  1. 转:Linux 文件IO理解

    源地址http://blog.csdn.net/lonelyrains/article/details/6604851 linux文件IO操作有两套大类的操作方式:不带缓存的文件IO操作,带缓存的文件 ...

  2. 转 漫谈linux文件IO

    在Linux 开发中,有几个关系到性能的东西,技术人员非常关注:进程,CPU,MEM,网络IO,磁盘IO.本篇文件打算详细全面,深入浅出.剖析文件IO的细节.从多个角度探索如何提高IO性能.本文尽量用 ...

  3. 漫谈linux文件IO

    在Linux 开发中,有几个关系到性能的东西,技术人员非常关注:进程,CPU,MEM,网络IO,磁盘IO.本篇文件打算详细全面,深入浅出.剖析文件IO的细节.从多个角度探索如何提高IO性能.本文尽量用 ...

  4. 【转】嵌入式Linux文件系统启动脚本及分析

    原文网址:http://www.linuxidc.com/Linux/2011-03/33728.htm 在内核初始化完成后,嵌入式linux 文件系统的启动过程主要包含以下几个步骤: 1. 执行/s ...

  5. linux 文件IO

    1.文件描述符 (1)文件描述符的本质是一个数字,这个数字本质上是进程表中文件描述符表的一个表项,进程通过文件描述符作为index去索引查表得到文件表指针,再间接访问得到这个文件对应的文件表.(2)文 ...

  6. <摘录>linux文件IO

    这篇文章写的比较全面,也浅显易懂,备份下.转载自:http://blog.chinaunix.net/uid-27105712-id-3270102.html 在Linux 开发中,有几个关系到性能的 ...

  7. 2.Linux文件IO编程

    2.1Linux文件IO概述 2.1.0POSIX规范 POSIX:(Portable Operating System Interface)可移植操作系统接口规范. 由IEEE制定,是为了提高UNI ...

  8. linux文件io与标准io

    文件IO实际是API,Linux对文件操作主要流程为:打开(open),操作(write.read.lseek),关闭(close). 1.打开文件函数open(): 涉及的头文件:  #includ ...

  9. Linux文件IO操作

    来源:微信公众号「编程学习基地」 目录 文件操作 Linux文件类型 Linux文件权限 修改文件权限 Linux error 获取系统调用时的错误描述 打印错误信息 系统IO函数 open/clos ...

  10. linux文件IO操作篇 (一) 非缓冲文件

    文件IO操作分为 2 种 非缓冲文件IO 和 缓冲文件IO 它们的接口区别是 非缓冲 open() close() read() write() 缓冲 fopen() fclose() fread() ...

随机推荐

  1. KMP算法,匹配字符串模板(返回下标)

    //KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...

  2. 2022春每日一题:Day 21

    题目:[SCOI2007]降雨量 这题比较坑,分几种情况,但是可以总起来说,分开写,两个月份都没出现,maybe,否则如果两个月份都大于[l+1,r-1]的最大值,如果两个月份差值=r-l输出,tru ...

  3. uwsgi 启动配置文件

    # uwsig使用配置文件启动 [uwsgi] # 项目目录 chdir=/myfiles/xxx/xxx/my_project # 指定项目的application module=my_projec ...

  4. <四>理解空间配置器allocator, 优化STL 中的Vector

    .在上一节我们实现的 MyVector存在哪些问题? 问题1 现在有Student类 class Student{ public: Student(){cout<<"构造Stud ...

  5. 模块/collections/random/time/datetime

    内容概要 模块--包的具体使用 编程思想介绍 软件开发--目录规范 常用模块介绍--collections模块 常用模块介绍--time.datetime 常用模块介绍--random 1.包的具体使 ...

  6. SpringCloud -Netflix 总结·

    springcloud 核心组件 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.智能路由.消息 ...

  7. mongorestore target dump invalid CreateFile dump The system cannot find

    问题 使用 mongorestore 指定 dump 文件夹,恢复数据报错. mongorestore target 'dump' invalid: CreateFile dump: The syst ...

  8. 记录一次PyQt5内存泄漏的问题解决

    前言 前几天利用python-mpv写了一个播放器,但是跑着跑着发现内存越来越大,经过我反复调试终于解决了这个问题. 解决思路 模块定位 首先我是一个模块一个模块测试的,这样可以尽快缩减出问题的代码范 ...

  9. linux配置 python 开发环境sublime text及一些使用心得

    前言 一直以来我都使用 sublime text 作为主流开发的 ide ,但其实我开始在我的 linux mint 系统使用 sublime text 配置 python3 的开发环境踩过的坑又何止 ...

  10. java.util.Date和java.util.Calendar

    Date date = new Date();//分配初始化一个Date()对象 Calendar cal = Calendar.getInstance();//获取一个基于当前时间的日历 int d ...