c/c++ 二進制文件讀寫

一下代碼本人都在VC++6.0環境下測試過。

1、windows api(MFC)

#include <stdio.h>
//#include <windows.h>
#include <afx.h>
//頭文件也可以使用#include<windows.h>,而不用#include<afx.h> 但代碼中的CString都要換成char*

typedef struct {
	char name[20];
	short int age;
} PERSON;

void WriteFile(CString fileName, PERSON* person);//char* fileName
void ReadFile(CString fileName, PERSON* person);

void main()
{
	PERSON person;
	strcpy(person.name, "zhangsan");
	person.age = 20;

	//Wtite 文件名後綴可以自己定
	WriteFile("person.per", &person);

	PERSON personCopy;
	//Read
	ReadFile("person.per", &personCopy);
	printf("name:%s age:%d",person.name, person.age);
	getchar();
}
void WriteFile(CString fileName, PERSON* person)
{
	HANDLE hFile = CreateFile(fileName,GENERIC_WRITE | GENERIC_READ, 
				0, NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

	if(hFile == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFile);
		return;
	}

	DWORD dwWrite = 0;
	if(!WriteFile(hFile,person,sizeof(PERSON),&dwWrite,NULL))
	{
		CloseHandle(hFile);
		return;
	}

	CloseHandle(hFile);
}

void ReadFile(CString fileName, PERSON* person)
{
	HANDLE hFile = CreateFile(fileName,GENERIC_WRITE|GENERIC_READ, 
		0, NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

	if(hFile == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFile);
		return;
	}

	DWORD dwRead = 0;
	if(!ReadFile(hFile,person,sizeof(PERSON),&dwRead,NULL))
	{
		CloseHandle(hFile);
		return;
	}

	CloseHandle(hFile);
}

2、c++

#include <fstream>
using namespace std;

typedef struct {
	char name[20];
	short int age;
} PERSON;

void WriteFile(char* fileName, PERSON* person);
void ReadFile(char* fileName, PERSON* person);

void main()
{
	PERSON person;
	strcpy(person.name, "zhangsan");
	person.age = 20;

	//Wtite 文件名後綴可以自己定
	WriteFile("person.per", &person);

	PERSON personCopy;
	//Read
	ReadFile("person.per", &personCopy);
	printf("name:%s age:%d",person.name, person.age);
	getchar();
}

void WriteFile(char* fileName, PERSON* person)
{
	ofstream outFile(fileName, ios::binary);
	outFile.write((char*)person, sizeof(PERSON));
	outFile.close();
}

void ReadFile(char* fileName, PERSON* person)
{
	ifstream infile(fileName, ios::binary);
	infile.read((char*)person,sizeof(PERSON));
	infile.close();
}

3、c

#include <stdio.h>
#include <string.h>

typedef struct {
	char name[20];
	short int age;
} PERSON;

void WriteFile(char* fileName, PERSON* person);
void ReadFile(char* fileName, PERSON* person);

void main()
{
	PERSON person;
	strcpy(person.name,"zhangsan");
	person.age = 20;

	//Wtite 文件名後綴可以自己定
	WriteFile("person.per", &person);

	PERSON personCopy;
	//Read
	ReadFile("person.per", &personCopy);
	printf("name:%s age:%d",person.name, person.age);
	getchar();
}

void WriteFile(char* fileName, PERSON* person)
{
	FILE* file;
	file = fopen(fileName, "wb");
	fwrite(person,sizeof(PERSON),1, file);
	fclose(file);
}

void ReadFile(char* fileName, PERSON* person)
{
	FILE* file;
	file = fopen(fileName, "rb");
	fread(person,sizeof(PERSON),1, file);
	fclose(file);
}




發佈了21 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章