c语言读写文件的各种操作
目录
- c语言读写文件的各种操作
- fopen() -- 打开文件
- fclose() -- 关闭文件
- fgetc() -- 读取一个字符
- fputc() -- 写入一个字符
- fgets() -- 读取一行
- fputs() -- 写入字符串到文件
- fread() -- 从文件读取指定大小的数据到数组
- fwrite() -- 把指定大小的数组写入到文件
- fscanf() -- 从文件读取数据并格式化输入
- fprintf() -- 格式化输出到文件
- ftell() -- 获取当前位置
- fseek() -- 改变当前位置
- fgetpos()、fsetpos() -- 标记位置、跳转到标记位
- rewind() -- 回到文件开头
- feof() -- 检查文件是否结束
- ferror() -- 测试错误标识符
- clearerr() -- 清除文件结束和错误标识符
- fflush() -- 刷新缓冲区
- freopen() -- 输出重定向
- remove() -- 删除文件
- rename() -- 文件重命名
- 笔记:
fopen() – 打开文件
函数:
fopen()
声明:
FILE *fopen(const char *filename, const char *mode)
作用:
使用给定的模式 mode 打开 filename 所指向的文件。要用fclose()函数关闭
参数:
filename -- 这是 C 字符串,包含了要打开的文件名称。
mode -- 这是 C 字符串,包含了文件访问模式,模式如下:
实例:
一般写法
FILE *P = NULL;
P = fopen("xxx.txt","r");
//fclose(P);
精简写法
FILE *P = fopen("xxx.txt","r");
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fopen.html
注意!:每次进行读取和写入数据操作后,记录当前位置的指针都会自动跳转到操作的数据后面。
fclose() – 关闭文件
实例:
FILE *P = fopen("xxx.txt","r"); //打开文件
fclose(P); //关闭文件
查看更多:
https://www.runoob.com/cprogramming/c-function-fclose.html
fgetc() – 读取一个字符
实例:
int c;
FILE *P = fopen("xxx.txt","r");
c = fgetc(P);
//printf("%c",c);
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fgetc.html
fputc() – 写入一个字符
实例:
FILE *P = fopen("file.txt", "w+");
int ch;
for( ch = 33 ; ch <= 100; ch++ )
{
fputc(ch, fp);
}
fclose(fp);
//输出:!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
查看更多:
https://www.runoob.com/cprogramming/c-function-fputc.html
fgets() – 读取一行
声明:
char *fgets(char *str, int n, FILE *stream)
参数:
str -- 这是指向一个字符数组的指针,该数组存储了要读取的字符串。
n -- 这是要读取的最大字符数(包括最后的空字符)。通常是使用以 str 传递的数组长度。
stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了要从中读取字符的流。
返回值:
如果成功,该函数返回相同的 str 参数。如果到达文件末尾或者没有读取到任何字符,str 的内容保持不变,并返回一个空指针。
如果发生错误,返回一个空指针。
实例:
char str[60];
FILE *P = fopen("xxx.txt","r");
fgets(str, 60, P);
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fgets.html
fputs() – 写入字符串到文件
作用:
把字符串写入到指定的流 stream 中,但不包括空字符。
实例:
FILE *P = fopen("file.txt","w+");
fputs("Hello, World!", P);
fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fputs.html
fread() – 从文件读取指定大小的数据到数组
声明
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
参数
ptr -- 这是指向带有最小尺寸 size*nmemb 字节的内存块的指针。
size -- 这是要读取的每个元素的大小,以字节为单位。
nmemb -- 这是元素的个数,每个元素的大小为 size 字节。
stream -- 这是指向 FILE 对象的指针,该 FILE 对象指定了一个输入流。
实例:
char str[10];
FILE *P = fopen("xxx.txt","r");
fread(str,1,1,P); //每次读取一个字节
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fread.html
fwrite() – 把指定大小的数组写入到文件
声明:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
参数:
ptr -- 这是指向要被写入的元素数组的指针。
size -- 这是要被写入的每个元素的大小,以字节为单位。
nmemb -- 这是元素的个数,每个元素的大小为 size 字节。
stream -- 这是指向 FILE 对象的指针,该 FILE 对象指定了一个输出流。
实例:
char str[] = "This is a test.";
FILE *P = fopen("xxx.txt","w");
fwrite(str,sizeof(str),1,P);
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fwrite.html
fscanf() – 从文件读取数据并格式化输入
实例:
char str[10];
int a;
FILE *P = fopen("xxx.txt","r");
fscanf(P,"%s %d",str,&a);
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fscanf.html
fprintf() – 格式化输出到文件
实例:
char str[] = "hello world!";
int a = 2020;
FILE *P = fopen("xxx.txt","r");
fprintf(P,"%s %d",str,a);
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fprintf.html
ftell() – 获取当前位置
实例:
ftell(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-ftell.html
fseek() – 改变当前位置
作用:
设置位置偏移,改变当前位置
声明:
int fseek(FILE *stream, long int offset, int whence)
参数
stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。
offset -- 这是相对 whence 的偏移量,以字节为单位。
whence -- 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一:
常量 | 描述 |
---|---|
SEEK_SET | 文件的开头 |
SEEK_CUR | 文件指针的当前位置 |
SEEK_END | 文件的末尾 |
实例:
char str[10];
FILE *P = fopen("xxx.txt","r");
fread(str,1,1,P); //每次读取一个字节
fseek(P,-2,SEEK_CUR); //偏移到上一个字节
fread(str,1,1,P); //读取上一个字节
//fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-fseek.html
fgetpos()、fsetpos() – 标记位置、跳转到标记位
声明:
int fgetpos(FILE *stream, fpos_t *pos)
int fsetpos(FILE *stream, const fpos_t *pos)
实例:
FILE *P = fopen("file.txt","w+");
fpos_t position;
fgetpos(P, &position);
fputs("Hello, World!", P);
fsetpos(P, &position);
fputs("这将覆盖之前的内容", P);
fclose(P);
// 输出:这将覆盖之前的内容
查看更多:
https://www.runoob.com/cprogramming/c-function-fgetpos.html
https://www.runoob.com/cprogramming/c-function-fsetpos.html
rewind() – 回到文件开头
声明:
void rewind(FILE *stream)
实例:
FILE *fp = fopen("file.txt","w");
fputs("0000",fp);
rewind(fp);
fputs("11",fp);
fclose(fp);
// 文件file.txt内容为1100
查看更多:https://www.runoob.com/cprogramming/c-function-rewind.html
feof() – 检查文件是否结束
作用:
检测流上的文件结束符的函数,如果文件结束,则返回非0值,否则返回0
实例:
FILE *P = fopen("xxx.txt","r");
//文件结束自动退出
while(!feof(P))
{
//具体操作
}
fclose(P);
查看更多:
https://www.runoob.com/cprogramming/c-function-feof.html
ferror() – 测试错误标识符
作用:
如果设置了与流关联的错误标识符,该函数返回一个非零值,否则返回一个零值。
声明:
int ferror(FILE *stream)
实例:
FILE *fp = fopen("file.txt", "w"); // 以只写模式打开file.txt
char c;
c = fgetc(fp); // 读取
if( ferror(fp) )
{
printf("读取文件:file.txt 时发生错误\n");
}
fclose(fp);
// 输出:读取文件:file.txt 时发生错误
// 因为file.txt是以只写模式打开的文件,所以读取会出现错误,ferror()用于判断该错误。
查看更多:https://www.runoob.com/cprogramming/c-function-ferror.html
clearerr() – 清除文件结束和错误标识符
作用:
当文件读取或写入出现错误时,最常见的是读到EOF导致出错
这时继续对文件指针进行读写会出错,包括移动文件指针
通过clearerr()可以清除这个错误,使文件操作可以继续执行
声明:
void clearerr(FILE *stream)
实例:
FILE *fp = fopen("file.txt", "w"); // 以只写模式打开file.txt
char c;
c = fgetc(fp); // 读取
if( ferror(fp) )
{
printf("读取文件:file.txt 时发生错误____1\n");
}
clearerr(fp); // 清除错误标识符
if( ferror(fp) )
{
printf("读取文件:file.txt 时发生错误____2\n");
}
fclose(fp);
// 输出:读取文件:file.txt 时发生错误___1
// 因为清除了错误标识符,所以第二次判断错误的时候没有执行
查看更多:https://www.runoob.com/cprogramming/c-function-clearerr.html
fflush() – 刷新缓冲区
作用:
刷新流 stream 的输出缓冲区。。。
声明:
int fflush(FILE *stream)
实例就不写了
可以参考一下:https://zhidao.baidu.com/question/369228772.html?sort=11&rn=5&pn=0#wgt-answers
其它:https://www.runoob.com/cprogramming/c-function-fflush.html
freopen() – 输出重定向
作用:
把一个新的文件名 filename 与给定的打开的流 stream 关联,同时关闭流中的旧文件。
声明:
FILE *freopen(const char *filename, const char *mode, FILE *stream)
实例:
FILE *fp;
printf("该文本重定向到 stdout\n"); // 这句话会显示到屏幕
fp = freopen("file.txt", "w+", stdout); // 把标准输出重定向到txt文件中
printf("该文本重定向到 file.txt\n"); //这句话不会在屏幕显示,在txt文件里
fclose(fp);
freopen("CON", "w", stdout); // 把标准输出重定向到屏幕
// 在windows下控制台的标准输出设备是CON,在linux下面则用/dev/console(可能吧。。)
printf("该文本重定向到 stdout\n"); // 这句话会在屏幕显示
查看更多:https://www.runoob.com/cprogramming/c-function-freopen.html
remove() – 删除文件
作用:
删除文件,如果成功,则返回零。如果错误,则返回 -1,并设置 errno。
声明:
int remove(const char *filename)
实例:
int ret = remove("file.txt"); // 删除该文件
if(ret == 0) // 判断是否删除成功
{
printf("文件删除成功");
}
else
{
printf("错误:不能删除该文件");
}
查看更多:https://www.runoob.com/cprogramming/c-function-remove.html
rename() – 文件重命名
作用:
重命名文件,如果成功,则返回零。如果错误,则返回 -1,并设置 errno。
声明:
int rename(const char *old_filename, const char *new_filename)
实例:
int ret = rename("1.txt","2.txt"); // 把1.txt重命名为2.txt
if(ret == 0) // 判断重命名是否成功
{
printf("文件重命名成功");
}
else
{
printf("错误:无法重命名该文件");
}
查看更多:https://www.runoob.com/cprogramming/c-function-rename.html
笔记:
读取速度:fgetc > fgets > fread > fscanf
写入速度:fputc > fputs > fwrite > fprintf