目录
1.文件IO
文件IO遵循POSIX标准,文件IO实际上使用的是系统调用函数
标准IO适用于操作普通文件,文件IO适用于任意类型文件。
标准IO中通过流唯一标识一个文件
文件IO通过文件描述符唯一标识一个文件
1.1文件描述符
文件描述符是一个按顺序分配的最小的非负整数,把中间的数据关了,再打开新的文件时,先补上之前删除的编号。
0 1 2 3 4,把2关了,再打开先补上2,再从5开始
单用户打开或者新建一个文件时,系统会向当前进程放回一个最小的可用的文件描述符
三个特殊的文件描述符:
标准输入 0
标准输出 1
标准错误 2
1.2文件IO相关系统调用函数
open()/close() 打开关闭文件
read()/write() 读写文件
lseek() 文件定
(1)打开文件 --open()
#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_CREAT:当打开的文件不存在时,用该选项创建新文件,并用第三个参数为其设置权限
O_EXCL:一般和O_CREAT联合使用,用于检测文件是否存在,存在则打开失败,不存在加上O_CREAT后打开成功
O_APPEND:以追加的方式打开文件,在文件末尾另起一行,继续写入(不加时,文件是一个一个覆盖,标准IO不用a打开时,是把原文件清空)
O_TRUNC:打开文件后会将:wq原文件清空,再从头写
第二个参数要多个宏时,用位或连接
O_RDONLY|O_CREAT
mode:当需要创建新文件时,需要用该参数给新建文件设置初始权限,一般设为0664(八进制)
初始权限&(~umask)=最终权限
一般umask设为002(八进制),取反后111 111 101,也就是不给其他用户写的权限。
返回值:成功返回文件描述符,失败返回-1
(2)关闭文件 --close()
#include <unistd.h>
int close(int fd);参数:fd 表示要关闭文件的文件描述符
返回值:成功返回0,失败返回-1.
(3)读写文件 --read()/write()
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);参数:fd:要操作的文件描述符
buf:用户自定义的缓冲区,用于存放读取的内容,数据的存放地址
count:请求读取的字节数
返回值:成功返回读到的字节数,失败返回-1,读完返回0
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);参数:fd:要操作的文件描述符
buf:用户自定义的缓冲区,用于存放准备写入的内容
count:请求写入的字节数
返回值:成功返回写入的字节数,失败返回-1
练习:用文件IO实现文件拷贝
/*===============================================
* 文件名称:mycp.c
* 创 建 者:
* 创建日期:2022年08月05日
* 描 述:
================================================*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define n 5
int main(int argc, char *argv[])
{
if(argc<3)
{
printf("Usage: %s<sec_filenamr><desc_filename>\n",argv[0]);
}
int fd1 = open(argv[1],O_RDONLY); //打开文件
int fd2 = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC ,0664);
if(fd1<0)
{
perror("open 1");
return -1;
}
if(fd2<0)
{
perror("open 2");
return -1;
}
char buf[n]={0}; //复制需要的缓冲区
int ret,ret2;
while((ret=read(fd1,buf,n))>0) //循环读取,读完返回0,出错返回-1
{
ret2=write(fd2,buf,ret); //循环写入
//ret2=write(fd2,buf,strlen(buf));
//memset(buf,0,n);
}
return 0;
}
(4)文件定位 --lseek()
#include <sys/types.h>
#include <unistd.h>off_t lseek(int fd, off_t offset, int whence);
参数:fd: 文件描述符
offset:偏移量,可负
whence: 基准点
SEEK_SET 定位在文件开头
SEEK_CUR 定位在当前位置
SEEK_END 定位在文件末尾
返回值:成功返回当前定位位置相对于文件开头的偏移量,失败返回-1
对图片进行加密解密操作
1.打开图片 --open
2.读取图片的前10个字节 ---read(改变格式头)
3.将读取的10个字节有序打乱 (头尾交换)
4.将文件指针偏移到开头 --lessk
5.将打乱的数据写入文件 --write
6.关闭文件 --close
代码执行一次加密,再执行一次解密
/*===============================================
* 文件名称:picture.c
* 创 建 者:
* 创建日期:2022年08月05日
* 描 述:
================================================*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd = open("1.bmp",O_RDWR);//1.打开文件
if(fd<0)
{
perror("open");
return -1;
}
char buf[10]={0},i,c;
read(fd,buf,10); //2.读取文件
for(i=0;i<5;i++) //3.打乱格式头
{
c=buf[i];
buf[i]=buf[9-i];
buf[9-i]=c;
}
lseek(fd,0,SEEK_SET);//4.指针偏移到开头
write(fd,buf,10); //5.将打乱的数据写入文件
close(fd); //6.关闭文件
return 0;
}