C语言文件操作



一、为什么使用文件?

当我们写通讯录程序录入信息时,程序一退出,所有的信息都消失不见了,下一次运行通讯录时又要重新录入信息,通讯录的可利用率大大降低,所以为了实现数据的持久化,就必须使用文件操作,把信息存储到磁盘中。

二、什么是文件

磁盘上的文件就是文件。
程序文件包括源程序文件.c,目标文件.obj,可执行文件.exe。
数据文件的文件内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件。

文件名

一个文件要有一个唯一的文件标识,以便用户识别和引用。
文件名包含三部分:文件路径+文件名主干+文件后缀。
例如:c:\code\test.txt

三、文件的打开和关闭

文件指针

文件指针FILE *

FILE* pf//文件指针变量

文件在读写之前应该先打开文件,文件在使用完毕后需要关闭。

FILE* fopen(const char * filename,const char * mode);
int fclose(FILE* stream);

文件的顺序读写

fputc函数的功能可以把数据显示到屏幕上 把输入字符的保存在文件中。

int fputc(char c,FILE* stream);
#include<stdio.h>
int main()
{
	FILE* pf = fopen("test.dat", "w");//往文件中写入内容
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fputc('b', pf);
	fputc('i', pf);
	fputc('t', pf);
	fclose(pf);
	pf = NULL;
	return 0;
}

往文件中顺序写入bit三个字符。

请添加图片描述

#include<stdio.h>
int main()
{
	fputc('b', stdout);
	fputc('i', stdout);
	fputc('t', stdout);
	return 0;
}

往标准输出流中写入字符。
请添加图片描述
fgetc函数的功能可以从文件中读入数据 输出至屏幕中。

#include<stdio.h>
int main()
{
	FILE* pf = fopen("test.dat", "r");//往文件中读出内容
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件
	int ret = fgetc(pf);
	printf("%c", ret);
	ret = fgetc(pf);
	printf("%c", ret);
	ret = fgetc(pf);
	printf("%c", ret);
	fclose(pf);
	pf = NULL;
	return 0;
}

请添加图片描述
fgetc从标准输入流读取信息

#include<stdio.h>
int main()
{
	//fgetc从标准输入流读取信息
	int ret = fgetc(stdin);
	printf("%c", ret);
	ret = fgetc(stdin);
	printf("%c", ret);
	ret = fgetc(stdin);
	printf("%c", ret);
	
	return 0;
}

请添加图片描述
fputs读入一行输入到文件中

#include<stdio.h>
int main()
{
	FILE* pf = fopen("test.dat", "w");//往文件中写入内容
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fputs("abcdef\n", pf);
	fputs("ghi", pf);

	fclose(pf);
	pf = NULL;
	return 0;
}

fgets读入一行输出到屏幕中

#include<stdio.h>
int main()
{
	char arr[10] = { 0 };
	FILE* pf = fopen("test.dat", "r");//往文件中写读出内容
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fgets(arr, 4, pf);
	printf("%s\n", arr);
	fgets(arr, 4, pf);
	printf("%s\n", arr);

	fclose(pf);
	pf = NULL;
	return 0;
}

格式化输入函数

#include<stdio.h>
struct S
{
	char arr[10];
	int num;
	float sc;

};
int main()
{
	struct S s = {0};//把结构体的数据读出到文件中
	//对格式化的数据进行读文件
	FILE* pf = fopen("test.dat", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fscanf(pf, "%s %d %f", s.arr, &s.num, &s.sc);//往文件中读信息
	printf("%s %d %f", s.arr, s.num, s.sc);//打印读到的信息
	fclose(pf);
	pf = NULL;
	return 0;
}

格式化输出函数

#include<stdio.h>
struct S
{
	char arr[10];
	int num;
	float sc;

};
int main()
{
	struct S s = { "abc",10,5.5f };//把结构体的数据写入到文件中
	//对格式化的数据进行写文件
	FILE* pf = fopen("test.dat", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fprintf(pf, "%s %d %f", s.arr, s.num, s.sc);//往文件中写入信息
	fclose(pf);
	pf = NULL;
	return 0;
}

fread 二进制读

#include<stdio.h>
struct S
{
	char arr[10];
	int num;
	float sc;

};
//把结构体的数据以二进制的形式写入到文件中
int main()
{
	struct S s = { 0 };
	FILE* pf = fopen("test.dat", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fread(&s,sizeof(struct S),1,pf);//从文件中读出信息
	//打印读出的信息
	printf("%s %d %f\n",s.arr,s.num,s.sc);
	fclose(pf);
	pf = NULL;
	return 0;
}

fwrite 二进制写

#include<stdio.h>
struct S
{
	char arr[10];
	int num;
	float sc;

};
//把结构体的数据以二进制的形式写入到文件中
int main()
{
	struct S s = { "abc",10,5.5f };
	FILE* pf = fopen("test.dat", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fwrite(&s,sizeof(struct S),1,pf);//往文件中写入信息
	fclose(pf);
	pf = NULL;
	return 0;
}

区别

scanf 针对标准输入的格式化的输入语句 stdin
fscanf 针对所有输入流的格式化输入语句 stdin/文件

printf 针对标准输出的格式化的输出语句 stdout
fprintf 针对所有输出流的格式化输出语句 stdout/文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值