文件编程之系统调用
来源:互联网 发布:洛带古城知乎 编辑:程序博客网 时间:2024/05/16 06:31
1、系统调用接口
大多数文件I/O只需要用到5个函数:open、read、write、lseek 以及 close。这些函数经常被称为不带缓冲的I/O(unbuffer I/O)。术语不带缓冲的是指每个read和write都直接调用内核的一个系统调用。
创建文件函数:
#include <fcntl.h> int creat(const char *pathname, mode_t mode);
pathname是创建后的文件的绝对路径。
mode是文件的访问权限,每个文件有9个访问权限位,分为三类,见下图。
打开文件函数:
#include <fcntl.h> int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);
open函数返回一个文件描述符fd,用于指定打开的文件流。 flags是打开标志,可由以下常量进行“或”运算构成flags参数。
注意:5个常量必须指定一个且只能指定一个!!
如果使用了O_CREAT标志,则使用的函数是:
int open(const char *pathname, int flags, mode_t mode);
例如(等价于 creat 函数):
open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
读文件函数
#include <unistd.h> ssize_t read(int fd,void *buf,size_t nbytes);
从文件描述符fd指定的文件中读取 nbytes 个字节到 buf 所指向的缓冲区中,返回值为实际读取的字节数。
写文件函数
#include <unistd.h> ssize_t write(int fd,const void *buf,size_t nbytes);
将 buf 指向的缓冲区内容写向文件描述符 fd 指定的文件中,返回值为实际写入文件的字节数。对于普通文件,写操作从文件的当前偏移量处开始,如果打开该文件时,指定了O_APPEND选项,则在每次写操作之前,将文件偏移量设置在文件的当前结尾处。
定位函数
#include <unistd.h> off_t lseek(int fd,off_t offset,int whence);
对参数offset的解释与参数whence的值有关。
若lseek成功执行,则返回新的文件偏移量,为此可以用下列方式确定打开文件的当前偏移量
关闭文件函数
#include <unistd.h> int close(int fd);
2、基于系统调用的文件拷贝demo
#include <apue.h>#include <fcntl.h>#define BUFSIZE 1024int copy(int psrc_fd, int pdst_fd);int main(int argc, const char *argv[]){ int src_fd; int dst_fd; if (argc != 3){ fprintf(stderr, "usage: %s src_file dst_file", argv[0]); exit(1); } /* 打开源文件 */ if ((src_fd = open(argv[1], O_RDONLY)) == -1){ fprintf(stderr, "open %s error !", argv[1]); exit(1); } /* 创建目的文件 */ if ((dst_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0751)) == -1){ fprintf(stderr, "open %s error !", argv[2]); exit(1); } /* 复制源文件到目的文件 */ copy(src_fd, dst_fd); /* 关闭文件 */ close(src_fd); close(dst_fd); exit(0);}int copy(int psrc_fd, int pdst_fd){ ssize_t bytes_read; ssize_t bytes_write; char buf[BUFSIZE]; char *ptr = NULL; /* 以下是经典的拷贝文件代码 */ while (bytes_read = read(psrc_fd, buf, BUFSIZE)){ /* 一个致命的错误发生了,跳出循环 */ if ((bytes_read == -1) && (errno != EINTR)){ fprintf(stderr, "Read data error from src_file !"); break; } else if (bytes_read > 0){ ptr = buf; while (bytes_write = write(pdst_fd, ptr, bytes_read)){ /* 一个致命的错误发生了,跳出循环 */ if ((bytes_write == -1) && (errno != EINTR)){ fprintf(stderr, "Write data error to dst_file !"); break; } /* 将所有读出的字节全部写入文件,跳出循环,继续读数据 */ else if (bytes_write == bytes_read){ break; } /* 若只写了一部分,继续写入文件 */ else if (bytes_write > 0){ ptr += bytes_write; //指向未读取数据的指针的首地址 bytes_read -= bytes_write; //计算剩下未读的字节数 } } } } return 0;}
阅读全文
0 0
- 文件编程之系统调用
- 文件编程之系统调用
- 文件编程之系统调用方式
- Linux文件编程之【系统调用】——绪论
- Linux文件编程之【系统调用】——creat()
- Linux文件编程之【系统调用】——open()
- Linux文件编程之【系统调用】——close()
- Linux文件编程之【系统调用】——read()
- Linux文件编程之【系统调用】——write()
- Linux文件编程之【系统调用】—— lseek()
- Linux文件编程之【系统调用】—— access()
- Linux文件编程之【系统调用】——绪论
- Linux文件编程之--库函数pk系统调用函数
- LS8-linux系统调用方式文件编程之学习笔记
- 文件编程总结(系统调用)
- LINUX文件编程-系统调用
- linux文件编程-系统调用
- 文件编程:系统调用方式
- 学习爬虫基础5-一个简单的小案例
- 数据传输的本质
- 【微信小程序 五】ListView上拉加载
- Hive自定义函数
- Android Studio Gradle 各个版本下载
- 文件编程之系统调用
- ES6中的const命令
- 明月固件SS 遍历配置文件输出SS服务器延迟
- sahi简单操作总结一
- sql语句
- 带着问题学习分布式系统之数据分片
- 远程调用rpc的时候 远程的异常如何处理
- 图像相似性度量——互信息计算Matlab实现
- java 操作 word 资料整理