Thanks for the info. So heres the revised example which contains both the c method and the c++ file stream method:
Code:
#define USE_STREAMS
enum sex {male, female};
struct data
{
char name[30];
sex MF;
int age;
double weight;
};
using namespace std;
int main(int argc, char *argv[])
{
int length;
struct data student[10];
#ifdef USE_STREAMS
ifstream isFile;
isFile.open ("student.data", ios::binary );
isFile.seekg (0, ios::beg);
// read data
for (int i=0; i<10; i++)
{
isFile.read ((char *)(&student[i]),sizeof(data));
}
isFile.close();
cout << "name\tsex\tage\tweight:" << endl;
for (int i=0; i<10; i++)
{
cout << student[i].name << "\t";
student[i].MF?(cout << "female\t"):(cout << "male\t");
cout << student[i].age << "\t" << student[i].weight << endl;
}
#else
FILE * sFile;
if (NULL == (sFile = fopen("student.data", "rb")))
{
cout << "Could not open file" << endl;
system("PAUSE");
return -1;
}
cout << "name\tsex\tage\tweight:" << endl;
for (int i=0; i<10; i++)
{
fread(&student[i], sizeof(data), 1, sFile);
cout << student[i].name << "\t";
student[i].MF?(cout << "female\t"):(cout << "male\t");
cout << student[i].age << "\t" << student[i].weight << endl;
}
#endif //USE_STREAMS
system("PAUSE");
return EXIT_SUCCESS;
}
kyle