C语言学习之格式化读文件

学习知识点

格式化文件读写。

练习代码

#include <stdio.h>
#include <vector>
#include <Windows.h>
using namespace std;
class FontObject
{
public:
	char Key[5];
	char Content[30];
	FontObject(char key[],char content[])
	{
		strcpy(Key,key);
		strcpy(Content,content);
	}
};

vector<FontObject *> FontList;
BOOL LoadFontObject()
{
	FILE *fp;
	char key[5]="";
	char content[30]="";

	fp=fopen("d:\\HongChen.mb","a+");
	if (!fp)
	{
		return FALSE;   
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s\t%s\n",key,content);
		FontList.push_back(new FontObject(key,content));
	}   
	printf("完成!\n");    
	fclose(fp);
	return TRUE;
}
int main(int argc,char *argv[])
{
	LoadFontObject();
	if (!FontList.empty())
	{
		vector<FontObject *>::iterator i;
		for (i=FontList.begin();i!=FontList.end();i++)
		{
			printf("%s\t%s\n",(*i)->Key,(*i)->Content);
		}

		for (i=FontList.begin();i!=FontList.end();i++)
		{
			delete *i;
		}
		FontList.clear();
	}
	return 0;
}
原文地址:https://www.cnblogs.com/shya/p/2336059.html