C语言文件读写-按块和随机方式 + perror()

按块读写文件

#include<stdio.h>
#include<string.h>
#pragma pack(show)

struct Hero
{
	char name[64];
	int age;
};

void test01()
{
	int i;
	
	FILE* f_read;
	struct Hero temp[4];
	//写文件
	FILE* f_write = fopen("./test3.txt","wb");

	struct Hero heros[]=
	{
		{"孙悟空",999},
		{"鲁班",5},
		{"达摩",79},
		{"貂蝉",99}

	};

	if(f_write == NULL)
		return;

	
	for(i = 0;i < 4;i++)
	{
		fwrite(&heros[i],sizeof(struct Hero),1,f_write);
	}
	fclose(f_write);

	//读文件
	//FILE* f_read = fopen("./test1.txt","r");
	f_read = fopen("./test3.txt","rb");
	if(f_read == NULL)
		return;

	fread(temp,sizeof(struct Hero),4,f_read);
	for(i = 0;i< 4;i++)
		printf("姓名:%s  年龄: %d\n",temp[i].name ,temp[i].age);
	fclose(f_read);
}


int main()
{
	test01();

	return 0;
}

格式化方式读写文件

#include<stdio.h>
#include<string.h>
#pragma pack(show)



void test01()
{
	int i;
	char temp[1024] = {0};
	FILE* f_read;
	
	//写文件
	FILE* f_write = fopen("./test4.txt","w");

	if(f_write == NULL)
		return;

	fprintf(f_write,"hello world %s","abcd");
	fclose(f_write);

	//读文件
	//FILE* f_read = fopen("./test1.txt","r");
	f_read = fopen("./test4.txt","r");
	if(f_read == NULL)
		return;

	while(!feof(f_read))
	{
		fscanf(f_read,"%s",temp);
		printf("%s\n",temp);
	}
	fclose(f_read);
}


int main()
{
	test01();

	return 0;
}

 运行结果:

hello
world
abcd
请按任意键继续. . .

注意这段代码:

while(!feof(f_read))
    {
        fscanf(f_read,"%s",temp);
        printf("%s\n",temp);
    }

如果将其改为:

while(!feof(f_read))
    {
        fscanf(f_read,"%s",temp);
        printf("%s",temp);
    }

运行结果:

helloworldabcd请按任意键继续. . .

rewind和fseek使用

#include<stdio.h>
#include<string.h>
#pragma pack(show)

struct Hero
{
	char name[64];
	int age;
};

void test01()
{
	int i;
	
	FILE* f_read;
	struct Hero temp;
	//写文件
	FILE* f_write = fopen("./test3.txt","wb");

	struct Hero heros[]=
	{
		{"孙悟空",999},
		{"鲁班",5},
		{"达摩",79},
		{"貂蝉",99}

	};

	if(f_write == NULL)
		return;

	
	for(i = 0;i < 4;i++)
	{
		fwrite(&heros[i],sizeof(struct Hero),1,f_write);
	}
	fclose(f_write);

	//读文件
	//FILE* f_read = fopen("./test1.txt","r");
	f_read = fopen("./test3.txt","rb");
	if(f_read == NULL)
		return;

	//移动光标
	//SEEK_SET 从开始   SEEK_END 从结尾   SEEK_CUR 从当前位置
	//fseek(f_read,sizeof(struct Hero)*2,SEEK_SET);
	fseek(f_read,-(int)sizeof(struct Hero)*2,SEEK_END);   //这块不能直接-sizeof(...),因为sizeof的返回值为无符号类型,加个负号没有意义,所以前面给他强转成long、int都行
	fread(&temp,sizeof(struct Hero),1,f_read);
	
	printf("姓名:%s  年龄: %d\n",temp.name ,temp.age);

	rewind(f_read); //将光标移到开始处
	fread(&temp,sizeof(struct Hero),1,f_read);
	
	printf("姓名:%s  年龄: %d\n",temp.name ,temp.age);
	fclose(f_read);
}


int main()
{
	test01();

	return 0;
}

 运行结果:

姓名:达摩  年龄: 79
姓名:孙悟空  年龄: 999
请按任意键继续. . .

perror()

#include<stdio.h>
#include<string.h>
#pragma pack(show)

struct Hero
{
	char name[64];
	int age;
};

void test01()
{
	int i;
	
	FILE* f_read;
	
	//写文件
	f_read = fopen("./test5.txt","rb");

	

	if(f_read == NULL)
	{
		//error 宏
		//printf("文件加载失败\n");
		perror("文件加载失败"); //用户提示信息 + 系统提示信息
		return;
	}

	
}


int main()
{
	test01();

	return 0;
}

运行结果:

 文件加载失败: No such file or directory
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/weixin_42596333/article/details/104525763