1. 程式人生 > >C++ 讀取檔案內容到data 結構體 structure

C++ 讀取檔案內容到data 結構體 structure

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

typedef struct {
	int	n;	
	char *data;
} BLOCK;

int main () {
	BLOCK block;
	
	ifstream infile ("test.txt", ios::binary);
	infile.read((char*)&block.n, sizeof(block.n));
	block.data = (char *)calloc(1, 1024); //allocate space for data
	infile.read( block.data, 1024);
	infile.close();
	cout << "data : " << block.data << endl;

	return 0;
}