⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fread_fwrite.cpp

📁 本程序介绍了如何用C语言快速读写磁盘文件。
💻 CPP
字号:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Data_Struct.h"

/*********************************************************************************
	fread(buffer,size,count,fp);  // size:number of bytes
	fwrite(buffer,size,count,fp); // count:number of size
	return the value of count.
*********************************************************************************/
void fread_fwrite_test()
{
	test_struct list_tmp[MAX_NUM_test_struct] = {0};
	test_struct list[MAX_NUM_test_struct]	= 
	{ 
		{"litong",'m',30},{"wangma",'f',21},{"hongja",'m',18},
		{"xiaomi",'f',30},{"zhaxue",'m',21},{"wuping",'f',18}
	};
	FILE *fp;
	int i,read_flag;

	if((fp = fopen( "E:\\C++_sample\\File_operating\\File_testing.txt", "w+" ))== NULL)	
	{
		printf("cannot open the file\n");
		exit(0);
	}
	for( i = 0; i < MAX_NUM_test_struct; i++ )
		if( fwrite(&list[i],sizeof(test_struct),1,fp) != 1 )
			printf("file write error!\n");

	rewind(fp);

#if 1
	for( i = 0; i < MAX_NUM_test_struct; i++ )
		if( fread(&list_tmp[i],sizeof(test_struct),1,fp) != 1 )
			printf("file read error!\n");
		else
			printf("%s\t%c\t%d\n",list_tmp[i].name,list_tmp[i].sex,list_tmp[i].age);

#else   
	while(1)
	{
		read_flag = fread(&list_tmp[0],sizeof(test_struct),1,fp);
		if( feof(fp) )
			break;

		if( read_flag != 1 )
			printf("file read error!\n");
		else 
			printf("%s\t\t%c\t\t%d\n",list_tmp[0].name,list_tmp[0].sex,list_tmp[0].age);
	}
#endif

	fclose(fp);

	return;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -