当前位置:网站首页>【C语言进阶】文件操作(一)
【C语言进阶】文件操作(一)
2022-06-29 09:36:00 【皓仔活在今天】
文章目录
1、程序文件
1、源文件(.c为后缀)
2、目标文件(Windows环境下后缀为.obj)
3、可执行程序(Windows环境下后缀为.exe)
2、数据文件
1、数据文件是在计算机系统上使用的最常见类型的文件之一。本质上,它可以是存储一个数据的任何文件。
2、可以用程序文件读取,或者写入数据文件中的数据
3、文件指针
1、使用数据文件读取或写入的时候,系统一般会将文件信息自动创建一个FILE结构体变量,并填充其中的信息,使用者不必关心细节
2、一般都是通过一个FILE*的指针变量来维护FILE这个结构体变量
3、定义pf是一个指向FILE类型数据的指针变量,可以是pf指向某个文件的文件信息区(是一个结构体变量)。通过文件信息区中的信息就能够访问该文件。也就是说,通过文件指针变量能够找到与它相关联的文件
4、fopen函数用于打开文件,fclose函数用于关闭文件
#include <errno.h>
#include <stdio.h>
int main()
{
FILE* pf = fopen("text.txt","w");
//打开文件
if (pf == NULL)
{
//printf("打开文件失败\n");
printf("%s\n",strerror(errno));
return 0;
}
//写文件
//关闭文件
fclose(pf);
pf = NULL;
//FILE* pf = fopen("C:\\2021code\\text.txt","w");
//如果想在特定路径生成文件,需要加入具体路径,
//使用两个\\是为了消除转移字符的影响
return 0;
}
5、打开文件夹的方式
“r”(只读):为了输入(把文件中的数据输入程序),打开一个已经存在的文本文件(如果指定文件不存在就打开错误,返回一个空指针)
“w”(只写):为了输出(把数据写入文件中),打开一个文本文件(如果指定文件不存在就新建一个文件)
“a”(追加):向文本文件尾添加数据(如果指定文件不存在就新建一个文件)
4、文件的顺序读写
stdout(标准输出流),stdin(标准输入流),stderr (标准错误流)
1、写一个字符到文件中
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写文件
//fputc('a', pf);
//fputc('b', pf);
//fputc('c', pf);
char ch = 0;
for (ch = 'a'; ch <= 'z'; ch++)
{
fputc(ch, pf);
}
fclose(pf);
pf = NULL;
return 0;
}
2、读文件中一个字符
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读文件
int ch = 0;
while ((ch = fgetc(pf)) != EOF)
{
printf("%c ", ch);
}
fclose(pf);
pf = NULL;
return 0;
}
3、写一行
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//fputs("hello world\n", pf);
//fputs("hehe\n", pf);
fputs("hello world\n", stdout);//fputs函数作用是写一行
//stdout是标准输出流,可以直接打印在屏幕上
fclose(pf);
pf = NULL;
return 0;
}
4、读一行
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
char buf[1000] = {
0 };
//读文件
fgets(buf, 1000, pf);//fgets函数作用是读一行
printf("%s", buf);
fclose(pf);
pf = NULL;
return 0;
}
5、拷贝文件代码
#include <stdio.h>
#include <errno.h>
int main()
{
//实现一个代码,将data.txt拷贝一份 生成data2.txt文件
FILE* pr = fopen("data.txt", "r");
if (pr == NULL)
{
printf("open for reading: %s\n", strerror(errno));
return 0;
}
FILE* pw = fopen("data2.txt", "w");
if (pw == NULL)
{
printf("open for writting: %s\n", strerror(errno));
fclose(pr);
pr = NULL;
return 0;
}
//拷贝文件
int ch = 0;
while ((ch = fgetc(pr)) != EOF)
{
fputc(ch, pw);
}
fclose(pr);
pr = NULL;
fclose(pw);
pw = NULL;
return 0;
}
6、写格式化的数据
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s = {
"张三",20,95.5 };
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写格式化的数据
fprintf(pf, "%s %d %lf", s.name, s.age, s.d);
//fprintf(stdout, "%s %d %lf", s.name, s.age, s.d);//写入到屏幕
fclose(pf);
pf = NULL;
return 0;
}
7、读格式化数据
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s = {
"张三",20,95.5 };
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读格式化的数据
fscanf(pf,"%s %d %lf", s.name, &(s.age), &(s.d));
printf("%s %d %lf", s.name, s.age, s.d);
fclose(pf);
pf = NULL;
return 0;
}
8、以二进制方式写
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s[2] = {
{
"张三",20,95.5 },{
"lisi",16,66.5} };
FILE* pf = fopen("data.txt", "wb");//wb是二进制写
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//按照二进制的方式写文件
fwrite(&s,sizeof(struct Stu),2,pf);
fclose(pf);
pf = NULL;
return 0;
}
9、以二进制方式读
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s[2] = {
0 };
FILE* pf = fopen("data.txt", "rb");//rb是二进制读
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//按照二进制的方式写文件
fread(&s,sizeof(struct Stu),2,pf);
printf("%s %d %lf\n", s[0].name, s[0].age, s[0].d);
printf("%s %d %lf\n", s[1].name, s[1].age, s[1].d);
fclose(pf);
pf = NULL;
return 0;
}
5、函数对比
1、scanf(从标准输入流(stdin)上进行格式化输入函数)
2、printf(向标准输出流(stdout)上进行格式化的输出函数)
3、fcanf(可以从标准输入流,或指定的文件流 上读取格式化的数据)
4、fprintf(把数据按照格式化的方式 输出到标准输出流,或指定文件流)
5、sscanf(可以从一个字符串中,提取或转化出格式化数据)
6、sprintf(把一个格式化的数据转换成字符串)
1、sscanf和sprintf的用法
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s = {
"张三",20,95.5 };
struct Stu tmp = {
0 };
char buf[100] = {
0 };
sprintf(buf, "%s %d %lf", s.name, s.age, s.d);
printf("%s\n", buf);
sscanf(buf,"%s %d %lf", tmp.name, &(tmp.age), &(tmp.d));
//buf这个字符串,以结构体形式提取出来,就是sscanf的功能
printf("%s %d %lf\n", tmp.name, tmp.age, tmp.d);
return 0;
}
边栏推荐
猜你喜欢
随机推荐
Dev使用过程中的基本操作
Agctfb partial solution
Analysis of liferayportal jsonws deserialization vulnerability (cve-2020-7961)
AGCTFb部分题解
《CLR via C#》读书笔记-单实例应用程序
Software test model (V model and W model)
在 2022 年找工作,毕业生们的 “最后一课”
全面理解Volatile关键字
Solve the problem that zxing's QR code contains Chinese garbled code
How to quickly complete disk partitioning
Basic operations during dev use
ssh密钥泄露(B模块赛题)——应用服务漏洞扫描与利用
Learn spark computing framework in practice (01)
Binary tree
DevExpress的双击获取单元格数据
在实践中学习Spark计算框架(01)
WinForm uses zxing to generate QR code
Recyclerview universal adapter package
Buuctf-- happy New Year
全面理解MESI缓存一致性协议








