政务移动门户网站建设园区二学一做网站
文章目录
- linux系统文件io
 - 1 open /close
 - 1.1 open
 - 1.2 close
 - 1.3 示例
 - 1.3.1 打开已经存在的文件
 
- 2 read/write
 - 2.1 read
 - 2.2 write
 - 使用
 
- 遗留问题:新创建的文件权限很奇怪
 - 3 lseek
 - 3.1 文件指针的移动
 - 3.2 文件拓展
 
- perror函数
 
linux系统文件io
系统函数是系统专有的函数,不是内核函数,内核函数是不允许用户使用的,所以系统函数来做这个桥梁调用它
1 open /close
1.1 open
这里纠结了很久,想直接去看c++的文件io,但是没有必要,文件操作的open/close用c标准就行了,所以回过头来看了
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h># 打开已经存在的文件
int open(const char* pathname, int flags);
# 打开文件,如果不存在会自动创建
int open(const char* pathname, int flags, mode_t mode) 
-  
pathname:文件名
 -  
flags:使用什么方式打开
- O_RDONLY
 - O_WRONLY
 - O_RDWR 
- O_APPEND:数据追加到尾部
 - O_CREAT:如果文件不存在就创建文件
 - O_EXCL:检查文件是否存在,必须和O_CREAT一起使用 O_CREAT|O_EXCL 
- 文件不存在,创建文件
 - 文件存在,创建失败,返回-1(如果不使用这个的话不会返回-1)
 
 
 
 -  
mode:在创建文件的时候指定权限,八进制整数,最大值为0777
- 还需要计算
 
 -  
返回值,打开成功返回文件描述符 打开失败返回-1
 
1.2 close
#include <unistd.h>
int close(int fd);
# fd就是open函数的返回值
 
1.3 示例
1.3.1 打开已经存在的文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
int main()
{int fd = open("./new.txt",O_WRONLY);if (fd == -1){perror("open:");}else{std::cout << "fd :" << fd;}close(fd);return 0;
}
 
1.3.2 创建不存在的文件并打开
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
int main()
{int fd = open("./add.txt",O_RDWR|O_CREAT|O_EXCL);if (fd == -1){perror("open:");std::cout << "file is exists." << std::endl;}else{std::cout << "fd :" << fd << std::endl;}close(fd);return 0;
} 
假设在创建新文件的时候, 给 open 指定第三个参数指定新文件的操作权限, 文件也是会被创建出来的, 只不过新的文件的权限可能会有点奇怪, 这个权限会随机分配而且还会出现一些特殊的权限位, 如下:
$ $ ll new.txt 
-r-x--s--T 1 robin robin 0 Jan 30 16:17 new.txt*   # T 就是一个特殊权限 
2 read/write
2.1 read
用于读取文件内部数据
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count); 
-  
buf 传出参数,只想一块有效内存
 -  
count buf指向的内存的大小,能存储的最大字节数
 -  
返回值
 -  
大于零:从文件中读取到的字节数
 -  
等于零:读取成功,但啥也没有
 -  
-1 :读取失败
 
2.2 write
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count); 
使用
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
int main()
{int fd = open("./new.txt",O_RDWR);if (fd == -1){perror("open:");}else{std::cout << "fd :" << fd << std::endl;}int fd2 = open("./new2.txt",O_WRONLY|O_EXCL|O_CREAT, 0777);if (fd2 == -1){perror("open:");}else{std::cout << "fd2 :" << fd << std::endl;}char buf[1024];int len = -1;while ( (len = read(fd, buf, sizeof(buf))) > 0){int ret = write(fd2, buf, len);if (ret == -1){perror("write");}}close(fd);close(fd2);return 0;
}
 
遗留问题:新创建的文件权限很奇怪
如果创建新的文件,要添加mode参数,并指定权限
3 lseek
lseek函数是系统函数,可以移动文件指针,进行文件拓展
#include <sys/types.h>
#include <unistd.h>off_t lseek(int fd, off_t offset, int whence);- offset 偏移量- whence 指定函数实现什么功能- SEEK_SET:从头部开始偏移offset个字节- SEEK_CUR:从当前文件指针的位置向后偏移offset- SEEK_END:从文件尾部向后偏移offset返回值:- 成功:返回从头部开始计算总的偏移量- 失败: -1
 
3.1 文件指针的移动
# 指针移动到文件头部
lseek(fd, 0, SEEK_SET);# 查看当前指针的位置
lseek(fd, 0, SEEK_CUR);# 得到文件的大小
lseek(fd, 0, SEEK_END);
 
3.2 文件拓展
拓展文件以后需要立即写入随便什么东西
	lseek(fd, 1000, SEEK_END);write(fd,"1", 1);
 
perror函数
#include <stdio.h>
void perror(const char *s)
 
实际使用的时候,当出现错误的时候会有对应的返回值,我们只知道返回值,至于它的错误是什么,可以调用perror自动输出,还可以添加提示信息s
if (flag == -1)
{perror("error1");close();
}
if (flag == "ss")
{perror("error2");break;
}
 
总之就是将函数报错的返回值翻译成错误并打印出来
