国内工程机械行业网站建设现状,ui是什么意思,jsp做网站用什么封装字符串,做像淘宝网的网站文章目录 linux系统文件io1 open /close1.1 open1.2 close1.3 示例1.3.1 打开已经存在的文件 2 read/write2.1 read2.2 write使用 遗留问题#xff1a;新创建的文件权限很奇怪3 lseek3.1 文件指针的移动3.2 文件拓展 perror函数 linux系统文件io
系统函数是系统专有的函数新创建的文件权限很奇怪3 lseek3.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_RDONLYO_WRONLYO_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.hoff_t lseek(int fd, off_t offset, int whence);- offset 偏移量- whence 指定函数实现什么功能- SEEK_SET:从头部开始偏移offset个字节- SEEK_CUR从当前文件指针的位置向后偏移offset- SEEK_END从文件尾部向后偏移offset返回值- 成功返回从头部开始计算总的偏移量- 失败 -13.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;
}总之就是将函数报错的返回值翻译成错误并打印出来