首先,先说一下标准IO和文件IO的区别
标准IO是库函数,是对系统调用的封装,而文件IO是系统调用,是Linux内核中的函数接口
标准IO是有缓存的,而文件IO是没有缓存
在Linux文件类型中,只有 - 这一类使用标准IO,文件类型不清楚的可以取翻看day01链接: link
这边简要说一下
b
c
d
- 标准IO
l
s
p
文件类型总共有7类,只有-这一类用标准IO
文件IO怎么进行操作
操作步骤
打开 -> 读/写 -> 关闭
打开文件
open
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_ASYNC 异步IO
O_CREAT 文件不存在创建
O_TRUNC 文件存在截断(清0)
返回值:
成功返回文件描述符(很小的非负整数)
失败返回-1
在标准IO中,读写文件共有六种,分别是"r",“r+”,“w”,“w+”,“a”,“a+”,在文件IO中,也可以写出这六种模式:
open("a.txt",O_RDONLY); //r
open("a.txt",O_RDWR); //r+
open("a.txt",O_WRONLY | O_CREAT | O_TRUNC,0664) //w
open("a.txt",O_RDWR | O_CREAT | O_TRUNC,0664) //w+
open("a.txt",O_WRONLY | O_CREAT | O_APPEND,0664)//a
open("a.txt",O_RDWR | O_CREAT | O_APPEND,0664) //a+
0664代表拥有者和组用户有读写权限;而其他用户只有读权限。
我们可以写一个例子:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<stdio.h>
int main(void)
{
int fd = 0;
fd = open("a.txt",O_RDONLY);
if(-1 == fd)
{
perror("fail to open!");
return -1;
}
printf("fd = %d\n",fd);
return 0;
}
输出结果:
为什么是3呢,是因为, 新生成的文件描述符总是为尚未被使用的最小的非负整数,而0是stdin,1是stdout,2是stderr。
如果在开始时关闭stdin,则fd=0,并不是二,因为新生成的文件描述符总是为尚未被使用的最小的非负整数,继续生成新文件的话,新文件则是3.
关闭文件
关闭文件:
close
int close(int fd);
功能:
将fd对应的文件描述符关闭
读写文件
write 写文件
ssize_t write(int fd, const void *buf, size_t count);
功能:
向fd对应的文件中写入buf指向的count个字节
参数:
fd:文件描述符
buf:写入数据空间首地址
count:写入的字节数
返回值:
成功返回实际写入字节数
失败返回-1
eg:向a.txt文件中写入字符串hello world!
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
int fd = 0;
char tmpbuff[4096] = {"hello world!"};
fd = open("a.txt",O_WRONLY | O_TRUNC | O_CREAT,0664);
if(-1 == fd)
{
perror("fail to open");
return -1;
}
write(fd,tmpbuff,strlen(tmpbuff));
close(fd);
return 0;
}
结果:
read 读文件
ssize_t read(int fd, void *buf, size_t count);
功能:
从文件描述符fd对应的文件中读取count个字节存放到buf开始的空间中
参数:
fd:文件描述符
buf:存放数据空间的首地址
count:想要读取数据字节数
返回值:
成功返回实际读到的字节数
失败返回-1
读到文件末尾返回0
eg:刚刚我们向a.txt文件中写入字符串hello world!,我们现在将其读出来
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
int fd = 0;
char tmpbuff[4096] = {0};
size_t ret = 0;
fd = open("a.txt",O_RDONLY);
if(-1 == fd)
{
perror("fail to open");
return -1;
}
ret = read(fd,tmpbuff,sizeof(tmpbuff));
printf("ret = %ld\n",ret);
printf("tmpbuff = %s\n",tmpbuff);
return 0;
}
结果:
刚刚写了一个练习题,利用read和write实现文件内容的拷贝(将src.jpg中的内容拷贝到dst.jpg文件中)
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<string.h>
int main(void)
{
int fsrc = 0;
int fdst = 0;
char tmpbuff[4096] = {0};
size_t nret = 0;
fsrc = open("src.jpg",O_RDONLY); //读文件
fdst = open("dst.jpg",O_WRONLY | O_CREAT | O_TRUNC,0664); //写文件
if(-1 == fsrc || -1 == fdst) //判断
{
perror("fail to open");
return -1;
}
while(1)
{
nret = read(fsrc,tmpbuff,sizeof(tmpbuff));
if(0 == nret)
{
break;
}
write(fdst,tmpbuff,nret);
}
close(fsrc);
close(fdst);
return 0;
}
如何测试是否拷贝成功呢,我们可以用ls -l查看字节数,如果相同,则拷贝成功