【c基础练习】文件练习

1.单字符读写

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main01()
{
	//写文件
	FILE* fp_write = NULL;
	//写方式打开文件
	fp_write = fopen("1.txt", "w+");
	if (fp_write == NULL)
	{
		perror("fopen:");
	}
	char buf[] = "this is a test for pfutc!";
	for (int i = 0; i < (int)strlen(buf); i++)
	{
		fputc(buf[i], fp_write);
	}
	fclose(fp_write);
	fp_write = NULL;

	//读文件
	FILE* fp_read;
	fp_read = fopen("1.txt", "r");
	if (NULL == fp_read)
	{
		return -1;
	}
	//判断文件结尾 注意:多输出一个空格
	while (!feof(fp_read))
	{
		printf("%c", fgetc(fp_read));
	}
	//char ch;
	//while ((ch=fgetc(fp_read))!=EOF)
	//{
	//	printf("%c", ch);
	//}
	system("pause");
	return EXIT_SUCCESS;
}


2.行读写

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main02()
{
//写文件
	FILE* fp_write = NULL;
	fp_write = fopen("1.txt", "w+");
	if (NULL == fp_write)
	{
		perror("fopen:");
		return -1;
	}
	char* buf[] = {
		"01 this is a test for pfutc!\n",
		"02 this is a test for pfutc!\n",
		"03 this is a test for pfutc!\n",
		"04 this is a test for pfutc!\n",
	};
	for (int i = 0; i < 4; i++)
	{
		fputs(buf[i], fp_write);
	}
	fclose(fp_write);
	fp_write = NULL;

//读文件
	FILE* fp_read = NULL;
	fp_read = fopen("1.txt", "r");
	if (NULL == fp_read)
	{
		perror("fopen:");
		return -1;
	}
	while (!feof(fp_read))
	{
		char temp[1024] = { 0 };
		fgets(temp, 1024, fp_read);
		printf("%s", temp);
	}
	fclose(fp_read);
	fp_read = NULL;

	system("pause");
	return EXIT_SUCCESS;
}


3.块读写

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct _TEACHER{
	char name[64];
	int age;
}Teacher;

int main03()
{
	FILE* fp_write= NULL;
	fp_write = fopen("1.txt", "wb");
	if (NULL == fp_write)
	{
		perror("fopen:");
		return -1;
	}
	Teacher teachers[4] = {
		{ "Obama", 33 },
		{ "John", 28 },
		{ "Edward", 45 },
		{ "Smith", 35 }
	};
	for (int i = 0; i < 4; i++)
	{
		fwrite(&teachers[i], sizeof(Teacher), 1,fp_write);
	}
	fclose(fp_write);
	fp_write = NULL;

	//读文件
	FILE* fp_read = NULL;
	fp_read = fopen("1.txt", "rb");
	if (fp_read == NULL)
	{
		perror("fopen:");
		return -1;
	}

	Teacher temps[4];
	fread(&temps, sizeof(Teacher), 4, fp_read);
	for (int i = 0; i < 4; i++){
		printf("Name:%s Age:%d\n", temps[i].name, temps[i].age);
	}

	fclose(fp_read);
	system("pause");
	return EXIT_SUCCESS;
}


4.格式化读写

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main04()
{
	//写文件
	FILE* fp_write = NULL;
	//写方式打开文件
	fp_write = fopen("1.txt", "w");
	if (fp_write == NULL)
	{
		perror("fopen:");
		return -1;
	}

	fprintf(fp_write, "hello world:%d!", 10);

	//关闭文件
	fclose(fp_write);

	//读文件
	FILE* fp_read = NULL;
	fp_read = fopen("1.txt", "rb");
	if (fp_read == NULL)
	{
		perror("fopen:");
		return -1;
	}

	char temps[1024] = { 0 };
	while (!feof(fp_read))
	{
		fscanf(fp_read, "%s", temps);
		printf("%s", temps);
	}

	fclose(fp_read);
	system("pause");
	return EXIT_SUCCESS;
}


习题

//1. 某个数据文件包含了家庭成员的年龄。一个家庭各个成员的年龄位于同一行,
//由空格分割。例如:下面的数据
//----------------------
//45 42 22
//36 35 7 3 1
//22 30
//----------------------
//描述了三个家庭的所有成员的年龄,它们分别有3个、5个和2个成员。
//编写一个程序,计算这种用文件表示的每个家庭所有成员的平均年龄。
//程序应该用格式代码%5.2f,打印出平均年龄,家庭每个成员的年龄。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
	//FILE* fp_write = NULL;
	//fp_write = fopen("作业01.txt", "w+");
	//if (NULL == fp_write)
	//{
	//	perror("open:");
	//	return -1;
	//}
	//char* buf[] = {
	//	"----------------------\n",
	//	"45 42 22\n"
	//	"36 35 7 3 1\n",
	//	"22 30\n",
	//	"----------------------\n",
	//};
	//for (int i = 0; i < 5; i++)
	//{
	//	fputs(buf[i], fp_write);
	//}
	//fclose(fp_write);
	//fp_write = NULL;
	FILE* fp_write = NULL;
	fp_write = fopen("1.txt", "w+");
	if (NULL == fp_write)
	{
		perror("fopen:");
		return -1;
	}
	char* buf[] = {
		"----------------------\n",
		"45 42 22\n"
		"36 35 7 3 1\n",
		"22 30\n",
		"----------------------\n",
	};
	for (int i = 0; i < 5; i++)
	{
		fputs(buf[i], fp_write);
	}
	fclose(fp_write);
	fp_write = NULL;
	getchar();
	return EXIT_SUCCESS;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值