既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
stream:文件指针。
3.用法
#include<stdio.h>
int main()
{
//以‘只写’的方式打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
//输入
for (int i = 'a'; i <= 'z'; i++)
fputc(i, pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.注意
用什么方式打开文件就只能完成该功能。就比如说,以‘只写’的方式打开文件,就不能进行读取操作。
如果对于打开方式模糊了,请点击下面链接,查看上一篇的内容:
3、fgetc
1.功能
从stream流中获取数据。
2.参数
文件指针类型的stream流。
3.用法
如果获取失败,函数将返回EOF
//以‘只读’的方式打开文件
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
//输出
int ch;
while((ch=fgetc(pf))!=EOF)
{
printf("%c ",ch);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4、fputs
1、功能
将一个字符串写入流中。
2、参数
str:需要写入文件的参数。
stream:文件流。
3、用法
如果没有‘\n’的话,他会把数据输入在一行。
如果把文件指针pf换成stdout的话,程序会把数据打印在屏幕上。
#include<stdio.h>
int main()
{
//以‘只写’的方式打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
fputs("abcd", pf);//不换行写法
fputs("abcd\n", pf);//换行写法
fputs("abcd", stdout);//将数据打印在屏幕上
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
5、fgets
1、功能
从流中获取字符串。
2、参数
str:字符串。
num:读取的个数。
stream:文件流。
3、用法
如果获取失败,函数将返回EOF
#include<stdio.h>
int main()
{
//以‘只读’的方式打开文件
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
char str[20];
fgets(str, 10, pf);
printf("%s", str);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4、注意
- 函数最多只会读取一行的数据。
- 当数据足够多时,参数为num,函数只会读取num-1个数据。
6、fprintf
1、功能
将格式化数据写入文件流中。
2、参数
通过将fprintf和printf的参数相比较,会发现,fprintf的参数就比printf的参数多了一个文件指针。
3、用法
#include<stdio.h>
struct S
{
float f;
char ch;
int n;
};
int main()
{
struct S s = { 4.14f,'w',1 };
//以‘只 写’的方式打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
printf("%f %c %d\n", s.f, s.ch, s.n);
fprintf(pf,"%f %c %d\n", s.f, s.ch, s.n);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
7、fscanf
1、功能
从流中读取数据。
2、参数
fscanf和scanf的参数差不多,多了一个文件流stream。
3、 用法
#include<stdio.h>
struct S
{
float f;
char ch;
int n;
};
int main()
{
struct S s = { 4.14f,'r',1 };
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
fscanf(pf, "%f %c %d", &(s.f), &(s.ch), &(s.n));
printf("%f %c %d", s.f, s.ch, s.n);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
8、fwrite
1、功能
将ptr中的数据写入流中,大小为size,个数为count。
2、参数
ptr:指向要写入文件流中的数据的指针。
size:每个数据的大小。
count:数据个数。
stream:要写入的文件流中。
3、用法
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "wb");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
char str[] = "abcd";
fwrite(str, sizeof(char), sizeof(str) / sizeof(str[0]), pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4、注意
文件的打开方式要和函数匹配,要用二进制的写的方式。
9、fread
1、功能
从文件流中读取数据。
2、参数
ptr:指向文件流中的数据所要存到的位置的指针。
size:每个数据的大小。
count:数据个数。
stream:文件流。
3、用法
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "rb");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
char str[20];
fread(str, sizeof(char), sizeof(str) / sizeof(str[0]), pf);
printf("%s", str);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4、注意
文件的打开方式要和函数匹配,要用二进制的读的方式。
三、文件的随机读写
根据文件指针的位置和偏移量来确定文件指针的位置。
文件指针当前位置
| SEEK_SET | Beginning of file |
| SEEK_END | Current position of the file pointer |
| SEEK_CUR | End of file |
1、fseek
功能是改变文件指针的位置。
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
int ch;
ch = fgetc(pf);//a
printf("%c\n", ch);
ch = fgetc(pf);//b
printf("%c\n", ch);
ch = fgetc(pf);//c
printf("%c\n", ch);
fseek(pf, -1, SEEK_CUR);
ch = fgetc(pf);
printf("%c\n", ch);//c
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
2、ftell
功能是返回文件当前指针的偏移量。
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
int ch;
ch = fgetc(pf);//a
printf("%c\n", ch);
ch = fgetc(pf);//b
printf("%c\n", ch);
ch = fgetc(pf);//c
printf("%c\n", ch);
![img](https://img-blog.csdnimg.cn/img_convert/0b1c24f6a01fbcd09e29c2c17ee075c4.png)
![img](https://img-blog.csdnimg.cn/img_convert/26895943d5a329c00d593515c9a0bda9.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**
test.txt", "r");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
int ch;
ch = fgetc(pf);//a
printf("%c\n", ch);
ch = fgetc(pf);//b
printf("%c\n", ch);
ch = fgetc(pf);//c
printf("%c\n", ch);
[外链图片转存中...(img-p4z2HGNz-1715712199368)]
[外链图片转存中...(img-4uheJKar-1715712199368)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**