【C语言进阶】文件操作(一)

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值