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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iostream>
#include <fstream>
using namespace std;
struct Student
{
char Name[20];
char ANumber[9];
int Age;
float GPA;
};
void studentPrint(Student [], int size);
int main(){
Student A[10];
Student C[10];
for (int i = 1; i <= 10; i++){
if (i >= 1 && i < 5){
strcpy_s(A[i].Name, 21, "Rob Smith");
strcpy_s(A[i].ANumber, 10, "A67241879");
A[i].Age = 12;
A[i].GPA = 3.49;
}
else{
strcpy_s(A[i].Name, 21, "John Doe");
strcpy_s(A[i].ANumber, 10, "A01241239");
A[i].Age = 17;
A[i].GPA = 2.90;
}
}
fstream file;
file.open("Record.bin", ios::out | ios::binary);
for (int j = 1; j <= 10; j++){
//If I write (char *) A[j] it says there is no suitable type conversion
file.write((char *) &A[j], sizeof(A));
//sizeof determines how far your cursor moves
}
file.close();
file.open("Record.bin", ios::in | ios::binary);
file.clear(); //use before seekg, or seekp to make sure you can move your cursor
file.seekg(0L, ios::beg);
for (int k = 1; k <= 10; k++){
//If I write (char *) C[k] it says there is no suitable type conversion
file.read((char *) &C[k], sizeof(C));
}
studentPrint(C, 10);
system("pause");
return 0;
}
void studentPrint(Student a[], int size){
for (int i = 1; i <= 10; i++){
cout << "Student Number " << i << " *****" << endl;
cout << "Student Name: " << a[i].Name << endl;
cout << "Student A Number: " << a[i].ANumber << endl;
cout << "Student Age: " << a[i].Age << endl;
cout << "Student GPA: " << a[i].GPA << endl;
cout << endl;
}
}
|