标准IO接口
1. fopen 和 fclose 函数
作用:打开指定文件和关闭指定文件
FILE * fopen ( const char *pathname, const char *type);
pathname : 想要打开的文件名称和路径
type :文件的打开方式
返回值 : 成功返回文件指针,失败返回NULL
FILE * fclose ( const char *pathname);
pathname : 想要打开的文件名称和路径
返回值:成功返回 0、 失败返回 EOF
常用的几种文件打开方式:
文件打开方式 | 含义 | 如果指定文件不存在 |
---|---|---|
r(只读) | 为了输入数据,打开一个已经存在的文本文件 | 出错 |
w(只写) | 为了输出数据,打开一个文本文件 | 建立一个新的文件 |
a(追加写) | 向文本文件尾添加数据 | 出错 |
rb(只读) | 为了输入数据,打开一个二进制文件 | 出错 |
wb(只写) | 为了输出数据,打开一个二进制文件 | 建立一个新的文件 |
ab(追加写) | 向一个二进制文件尾添加数据 | 出错 |
r+(读写) | 为了读和写,打开一个文本文件 | 出错 |
w+(读写) | 为了读和写,打开一个新的文件 | 建立一个新的文件 |
a+(读写) | 打开一个文件,在文件尾进行读写 | 建立一个新的文件 |
rb+(读写) | 为了读和写打开一个二级制危机 | 出错 |
wb+(读写) | 为了读和写,新建一个新的二进制文件 | 建立一个新的文件 |
ab+(读写) | 打开一个二进制文件,在文件尾进行读和写 | 建立一个新的文件 |
用法:
#include<stdio.h>
int main()
{
FILE* fp;
fp = fopen("./MYFILE.text","w"); //用只写的方式打开当前目录下的 MYFILE.text 文件
if(!fp)
printf("Fopen error!\n");
fclose(fp); //关闭文件
return 0;
}
2. fwrite 函数
作用:向指定文件写入一个数据块
size_t fwrite(const void* buffer,size_t size, size_t count, FILE* stream);
返回值:返回实际写入的数据块数。
参数:
- buffer是一个指针。对fwrite来说是要写入数据的地址。
- size是要写入内容的单字节数。
- count是要进行写入size字节的数据项的个数。
- stream为目标文件指针
用法:
#include<stdio.h>
#include<string.h>
int main()
{
FILE* fp;
fp = fopen("./MYFILE.text","w"); //用只写的方式打开当前目录下的 MYFILE.text 文件
if(!fp)
printf("Fopen error!\n");
int n = 5;
const char* p = "Hello World";
while(n--)
fwrite(p,strlen(p),1,fp);
fclose(fp); //关闭文件
return 0;
}
3. fseek 函数
作用:根据文件指针的位置和偏移量来定位文件指针
int fseek ( FILE * stream, long offset, int whence)
stram : 文件指针
offset : 偏移量(可正可负)
whence : 偏移参数(只有3项)
参数:
- SEEK_SET、从文件的头部位置开始偏移。
- SEEK_CUR、从当前读写位置开始偏移。
- SEEK_END、从文件的末尾位置开始偏移。
用法:
#include<stdio.h>
int main()
{
FILE* fp;
fp = fopen("./MYFILE.text","w"); //用只写的方式打开当前目录下的 MYFILE.text 文件
if(!fp)
printf("Fopen error!\n");
fputs("Hello World\n",fp); //将字符串输入文件中
fseek(fp,11,SEEK_SET); //从文件开头处偏移11个字节
fputs(”leihoua~~",fp);//将字符串输入到文件偏移处后
fclose(fp); //关闭文件
return 0;
}
结果如下所示:
4. fread 函数
作用:从一个文件流中读取数据
size_t fread(void *buffer, size_t size, size_t count, FILE *stream);
– buffer:用于接收数据的单元的首地址
– size:每个数据的大小
– count:数据个数
– stream:文件指针
注意:返回值随着调用格式的不同而不同
用法:
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *stream;
char msg[] = "this is a test";
char buf[20];
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
{
fprintf(stderr,
"Cannot open output file.\n");
return 1;
}
fwrite(msg, strlen(msg), 1, stream);
fseek(stream, SEEK_SET, 0);
fread(buf, strlen(msg),1, stream);
printf("%s\n", buf);
fclose(stream);
return 0;
}
5. fputs 函数
作用:向文件中写入数据
int fputs(const char *s, FILE *stream);
s : 要写入的数据的地址
stream : 要写入的文件名指针
用法:
#include<stdio.h>
int main()
{
FLIE* fp = fopen("mb.text","w"); //有则打开,没则创建文件
char* s = "Hello";
fputs(s,fp);
fclose(fp);
return 0;
}
6. fgets 函数
作用:读取文件中的数据
char * fgets ( char * s, int size, FILE * stream)
s:数据存储位置
size:读取字符串的 size - 1 个字节
stream:文件名指针
用法:
#include<stdio.h>
int main()
{
FILE* fp = fopen("mb.text","r"); //有则打开,没则报错
char buf[20];
fgets(buf,6,fp);
printf("%s\n",buf);
fclose(fp);
return 0;
}
系统调用IO接口
1. open 和 close 函数
作用:打开一个文本文件
int open (char * fpname, int flag, mode_t mode);
fpname : 要操作的文件名
flag :文件的打开方式
mode: 创建文件的权限
返回值:成功—>文件操作符,失败—> -1
int close (int fd)
fd : 要关闭文件的文件描述符
返回值:成功---->0, 失败—> -1
文件的打开方式 | 含义 |
---|---|
O_RDONLY | 只读 |
O_WRNOLY | 只写 |
O_RDWR | 读写 |
O_CREAT | 有则打开,无则创建(可和以上3个合用) |
O_EXCL 与 O_CREAT 一同使用 | 文件存在则报错,没有则创建 |
O_TRUNC | 打开文件并且清空 |
O_APPEND | 在文件的末尾进行追加写 |
用法:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int fd = open("./pp.text","O_RDONLY|O_CREAT,0777);//有则打开,无则创建新文件
close(fd);
return 0;
}
2. write 函数
作用:向文件中写入数据
size_t write ( int fd, char * data, size_t len);
fd : 文件描述符
data : 要写入的数据的首地址
len : 写入文件的长度
返回值: 成功返回实际写入的字节长度,失败返回 -1
用法:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int fd = open("mb.text",O_RDWR|O_CREAT);
char* fg = "希望疫情快点结束啊~~~";
write(fd,fg,strlen(fg)); //因为没有用lseek,所以是覆盖写
close(fd);
return 0;
}
3. read 函数
作用:读取文件中的数据
size_t read ( int fd, char * buf , size_t len);
fd : 文件描述符
buf : 存放读取到的数据的缓冲区的首地址
len : 读取的数据的字节长度
返回值: 成功返回实际读取的字节长度、失败反回 -1
用法:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int fd = open("mb.text",O_RDWR|O_CREAT);
char* buf[20];
read(fd,buf,sizeof(buf);
printf("%s\n",buf);
close(fd);
return 0;
}
4. lseek 函数
作用:根据文件指针的位置和偏移量来定位文件指针
off_t lseek ( int fd, off_t offset, int whence)
fd : 文件描述符
offset : 偏移量(可正可负)
whence : 偏移参数(只有3项)
返回值:成功返回当前相对起始位置的偏移量、失败返回 -1
参数:
- SEEK_SET、从文件的头部位置开始偏移。
- SEEK_CUR、从当前读写位置开始偏移。
- SEEK_END、从文件的末尾位置开始偏移。
用法:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <fcntl.h>
#include<string.h>
int main()
{
int fd = open("mb.text",O_RDWR|O_CREAT);
lseek(fd,0,SEEK_END); //从末尾开始偏移0个字节
char* fg = "为疫情中牺牲的英雄默哀";
write(fd,fg,strlen(fg));
close(fd);
return 0;
}