1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <cstdio> // correct C++ names for C headers
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i = 12345;
int j = 54321;
char c = 'D';
{
// this bit writes record to file
FILE *pfile;
pfile = fopen("test1.dat", "wb");
fwrite(&i, sizeof(i), 1, pfile);
fwrite(&j, sizeof(j), 1, pfile);
fwrite(&c, sizeof(c), 1, pfile);
fclose(pfile);
}
int i2 = 0, j2 = 0;
char c2 = 'F';
{
// this bit reads record from file
FILE *rfile;
rfile = fopen("test1.dat", "rb");
fread(&i2, sizeof(i2), 1, rfile);
fread(&j2, sizeof(j2), 1, rfile);
fread(&c2, sizeof(c2), 1, rfile);
fclose(rfile);
}
cout << "i(" << i << ")=" << i2
<< " j(" << j << ")=" << j2
<< " c(" << c << ")=" << c2 << endl;
}
| |