目录
文件IO
5个函数:打开、关闭、读写、定位操作
1. 概念:由系统提供的一组用于输入输出的函数接口
2. 特点:
1. 没有缓冲机制,每次访问都要经过系统调用(系统向上提供的一组接口)
2. 围绕文件描述符进行操作,文件描述符是非负整数(>=0),依次加1进行分配
3. 文件IO默认打开三个描述符:0(标准输入)、1(标准输出)、2(标准出错)
4. 可以操作-、b、c、p、s、l(除目录以外所有文件);
补充:man 数字 函数名 1-9,
1:命令 2:系统调用 3:库函数
一,打开文件open
int open(const char *pathname, int flags);
int fd;
fd = open("./test.c",O_RDONLY);
-----------------------------------
fd = open("./test.c",O_WRONLY|O_CREAT|O_TRUNC,0666);
1.功能:打开文件
2.参数:pathname->文件路径名
flags->打开文件的方式O_RDONLY:只读O_WRONLY:只写O_RDWR:可读可写O_CREAT:创建O_TRUNC:清空O_APPEND:追加
3.返回值:成功->
文件描述符
失败->-1
当第二个参数中有O_CREAT选项时,需要给open函数传递第三个参数,指定创建文件的权限 int open(const char *pathname, int flags, mode_t mode);用八进制表示:0666;
创建出来的文件权限为指定权限值&(~umask)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int fd;
fd = open("./test.c",O_RDONLY);
if(fd < 0)
{
perror("open err");
return -1;
}
printf("fd:%d\n",fd);
close(fd);
return 0;
}
---------------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int fd;
fd = open("./test.c",O_WRONLY|O_CREAT|O_TRUNC,0666);
if(fd < 0)
{
perror("open err");
return -1;
}
printf("fd:%d\n",fd);
close(fd);
return 0;
}
二,关闭文件close
int close(int fd);
1.功能:关闭文件
2.参数:fd:文件描述符
三,读写文件read/write
1.ssize_t read(int fd, void *buf, size_t count);
s = read(fd,buf,32);
①功能:从一个已打开的可读文件中读取数据
②参数:fd->文件描述符
buf->存放位置
count->读取的个数
③返回值:成功->实际读到的个数
返回-1->表示出错,并设置errno号
返回0->表示读到文件结尾
2.ssize_t write(int fd, const void *buf, size_t count);
write(fd,buf,s);//从read函数中返回的读取的字符数s
①功能:向指定文件描述符中,写入 count个字节的数据。
②参数:fd->文件描述符
buf->要写的内容
count->要写入的字节个数
③返回值:成功->实际写入数据的个数
失败 -> -1
读取文件内容并写入文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int fd;
char buf[32] = "";
ssize_t s;
fd = open("./test.c",O_RDWR);
if(fd < 0)
{
perror("open err");
return -1;
}
printf("fd:%d\n",fd);
//读取文件内容
s = read(fd,buf,32);
printf("%d %s\n",s,buf);
//将文件内容写入文件
write(fd,buf,s);
close(fd);
return 0;
}
四,文件定位操作lseek
off_t lseek(int fd, off_t offset, int whence);
//定位
lseek(fd,10,SEEK_SET);
//计算长度
off = lseek(fd,0,SEEK_CUR);
①功能:设定文件的偏移位置
②参数:fd->文件描述符
offset偏移量正数->向文件结尾位置移动负数->向文件开始位置whence->相对位置SEEK_SET 开始位置SEEK_CUR 当前位置SEEK_END 结尾位置
③返回值:成功->文件的当前位置
失败-> -1
在第十一位插入字符串a;
计算文件内容长度
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int fd;
off_t off;
fd = open("./test.c",O_RDWR);
if(fd < 0)
{
perror("open err");
return -1;
}
//定位
lseek(fd,10,SEEK_SET);
//插入
write(fd,"a",1);
//计算长度
off = lseek(fd,0,SEEK_CUR);
printf("%ld\n",off);
return 0;
}
五,标准IO和文件IO的区别
标准IO
|
文件IO
| |
概念
|
C库中定义的一组输入输出的函数
|
系统提供的一组输入输出的函数
|
特点
|
1.有缓冲机制
2.围绕流进行操作
3.默认打开3个流:stdin,stdout,stderr
4.可以操作普通文件
|
1.没有缓冲机制
2.围绕文件描述符进行操作
3.默认打开3个文件描述符:
0,1,2
4.可以操作任意类型的文件
|
函数接口
|
1.打开文件:fopen/freopen
2.关闭文件:fclose
3.读写文件:fgetc/fputc,fgets/fputs,
fread/fwrite
4.文件定位:fseek/rewind/ftell
|
1.打开文件:open
2.关闭文件:close
3.读写文件:read/write
4.文件定位:lseek
|
如果本文中存在代码逻辑,代码完善,概念解释不通或不清楚的错误,请批评指正。