第1个回答 2012-07-10
#include <iostream>
using namespace std;
typedef struct hardware{
int num;
int amount;
int price;
}instruction;
int main()
{
instruction code[100] = {0};
for ( int j=0; j < 100; ++j )
{
code[j].amount += j*2;
code[j].num += j*4;
code[j].price += j*8;
}
FILE* hbin = fopen("hbin.txt", "w");
for (j = 0; j < 100; j++)
fwrite(&code[j], sizeof(instruction), 1, hbin);
fclose(hbin);
ifstream iFile("hbin.txt", ios::binary);
if ( !iFile )
{
cout<<"open file error!"<<endl;
return -1;
}
iFile.seekg(0,ios::end);
int iFileSize = iFile.tellg();
iFile.seekg(0,ios::beg);
char* psbBuf = new char[iFileSize];
memset(psbBuf, 0, iFileSize);
iFile.read(psbBuf,iFileSize);
instruction* pware = (instruction*)psbBuf;
instruction ac[100] = {0};
for ( int i =0; i < iFileSize/sizeof(instruction); ++i)
{
ac[i].amount=pware->amount;
ac[i].num=pware->num;
ac[i].price=pware->price;
cout<<ac[i].amount<<endl;
++pware;
}
return 0;
}本回答被提问者和网友采纳
第4个回答 2012-07-10
1.
FILE *hbin = fopen("hbin.txt", "r");
fread(code, sizeof(instruction), 100, hbin);
fclose(hbin);