C language file reading and writing-block and random way + perror()

Read and write files in blocks

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

Formatting to read and write files

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

 operation result:

hello
world
abcd
please press any key to continue...

Note this code:

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

If you change it to:

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

operation result:

helloworldabcd please press any key to continue...

Use of rewind and 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;
}

 operation result:

Name: Bodhidharma Age: 79
Name: Monkey King Age: 999
Please press any key to continue...

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

operation result:

 File loading failed: No such file or directory
Please press any key to continue...

 

 

Guess you like

Origin blog.csdn.net/weixin_42596333/article/details/104525763