C语言文件操作

1. fgetc函数和fputc函数

1.1字符输入函数fgetc

#include <stdio.h>
#include<string.h>
#include<errno.h>
int main()
{
	FILE* pf = fopen("D:\\C语言\\test.txt", "r");//绝对路径要防止转义
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	printf("%c", fgetc(pf));//读取文件并打印出来
	printf("%c", fgetc(pf));
	printf("%c", fgetc(pf));
	fclose(pf);
	pf = NULL;
	return 0;
}

1.2字符转出函数fputc

#include <stdio.h>
#include<string.h>
#include<errno.h>
int main()
{
	FILE* pf = fopen("D:\\C语言\\TEXT.txt", "w");//绝对路径要防止转义
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	fputc('b', pf);//将b i t写到文件中去
	fputc('i', pf);
	fputc('t', pf);
	fclose(pf);
	pf = NULL;
	return 0;
}

2. fgets函数和fputs函数

2.1 文本行输入函数fgets

#include <stdio.h>
#include<string.h>
#include<errno.h>
int main()
{
	char buf[1024] = { 0 };
	FILE* pf = fopen("D:\\C语言\\test.txt", "r");//绝对路径要防止转义
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	fgets(buf, 1024, pf);//读取文件
	printf("%s", buf);
	fgets(buf, 1024, pf);
	printf("%s", buf);
	fclose(pf);
	pf = NULL;
	return 0;
}

2.2 文本行输出函数fputs

#include <stdio.h>
#include<string.h>
#include<errno.h>
int main()
{
	char buf[1024] = { 0 };
	FILE* pf = fopen("D:\\C语言\\TEST1.txt", "w");//绝对路径要防止转义
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	fputs("hello\n", pf);//需要自己添加换行
	fputs("world\n", pf);
	fclose(pf);
	pf = NULL;
	return 0;
}

3. fscanf函数和fprintf函数

3.1 格式化输入函数fscanf

#include<stdio.h>
#include<string.h>
#include<errno.h>
struct s
{
	int n;
	float score;
	char arr[10];
};
int main()
{
	struct s stu = {0};
	FILE* pf = fopen("D:\\C语言\\test.txt", "r");//绝对路径要防止转义
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	fscanf(pf, "%d %f %s", &(stu.n), &(stu.score), stu.arr);
	printf("%d %f %s", &(stu.n), &(stu.score), stu.arr);
	fclose(pf);
	pf = NULL;
	return 0;
}


3.2 格式化输出函数fprintf

#include<stdio.h>
#include<string.h>
#include<errno.h>
struct s
{
	int n;
	float score;
	char arr[10];
};
int main()
{
	struct s stu = { 100,3.14f,"bit" };
	FILE* pf = fopen("D:\\C语言\\TEST2.txt", "w");//绝对路径要防止转义
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	fprintf(pf, "%d %f %s", stu.n, stu.score, stu.arr);//以格式化的形式写文件
	fclose(pf);
	pf = NULL;
	return 0;
}

4. sscanf函数和sprintf函数

sscanf函数是从字符串中读取格式化的数据;sprintf函数是把格式化数据输出成字符串。

4.1 函数sscanf

#include<stdio.h>
struct s
{
	int n;
	float score;
	char arr[10];
};
int main()
{
	struct s stu = { 100,3.14f,"abcdef" };
	struct s tmp = { 0 };
	char buf[1024] = { 0 };
	sscanf(buf, "%d %f %s", &(tmp.n), &(tmp.score), tmp.arr);
	printf("%d %f %s\n", tmp.n, tmp.score, tmp.arr);
	return 0;
}

4.2 函数sprintf

#include<stdio.h>
struct s
{
	int n;
	float score;
	char arr[10];
};
int main()
{
	struct s stu = { 100,3.14f,"abcdef" };
	char buf[1024] = { 0 };
	sprintf(buf, "%d %f %s", stu.n, stu.score, stu.arr);
	printf("%s\n", buf);
	return 0;
}

5. ftell函数和rewind函数

ftell函数是返回文件指针相对于起始位置的偏移量;rewind函数是让文件指针的位置回到文件的起始位置。

6.文件结束判定feof

牢记:在文件读取过程中,不能用feof函数的返回值直接判断文件的是否结束,而是应用于当文件读取结束的时候,判断是读取失败结束还是遇到文件尾结束。

#include<stdio.h>
int main()
{
	FILE* pf = fopen("D:\\C语言\\test.txt", "r");
	if (pf == NULL)
	{
		perror("open file");//错误报告函数
		return 0;
	}
	int ch = 0;
	while (ch = fgetc(pf) != EOF)
	{
		putchar(ch);
	}
	if (ferror(pf))
	{
		printf("error\n");
	}
	else if(feof(pf))
	{
		printf("end of file\n");
	}
	fclose(pf);
	pf = NULL;
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值