Dec 14, 2015 at 3:58am UTC
I am trying to write students to a binary file then read from the file and fill the mystudentVector. I am not sure where to begin. I am getting it to write to a file, it's the read that I'm having problems with.
This is just part of my code :
#ifndef STUDENT_HPP_
#define STUDENT_HPP_
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include "utilities.hpp"
fstream yourFile;
fstream aFile;
const int MAXROWS = 100;
//***********STUDENT ENUMS*************//
enum college {
LPC,
SFO,
BRK,
SLU,
WASHU,
CSC,
};//enum college
enum degreeMajor {
CS,
CIS,
MTH,
MUS,
BUIS,
VIT,
PHY,
ASTR,
ENG,
};//enum degreeMajor
//*********STUDENT STRUCTURES**********//
struct student {
int stuID; //4 characters long
string fname; //23 characters long
string lname; //29 characters long
college attendingCollege;
degreeMajor major;
double gpa; //0.0-4.0
double age; //14-90
double grades[MAXROWS]; //Each 0-100
int totalGrades;
};//struct student
struct Cstudent {
int stuID;
char fname[24];
char lname[30];
college attendingCollege;
degreeMajor major;
double gpa;
double age;
double grades[MAXROWS];
int totalGrades;
};//struct Cstudent
//*****STUDENT FUNCTION PROTOTYPES*****//
college inputCollege(void);
degreeMajor inputMajor(void);
string collegeToString(college);
string majorToString (degreeMajor);
void readStudents(vector<student> &, student, Cstudent, fstream &);
void writeStudents(vector<student> &, student, Cstudent, fstream &);
//**********STUDENT FUNCTIONS**********//
degreeMajor inputMajor(void){
int anInt;
cout << "0. CS\n";
cout << "1. CIS\n";
cout << "2. MTH\n";
cout << "3. MUS\n";
cout << "4. BUIS\n";
cout << "5. VIT\n";
cout << "6. PHY\n";
cout << "7. ASTR\n";
cout << "8. ENG\n";
anInt = enterInt("Please enter what major the student is pursuing: ", 0, 8);
return static_cast<degreeMajor>(anInt);
}//inputMajor
college inputCollege(void){
int anInt;
cout << "0. LPC\n";
cout << "1. SFO\n";
cout << "2. BRK\n";
cout << "3. SLU\n";
cout << "4. WASHU\n";
cout << "5. CSC\n";
anInt = enterInt("Please enter what school the student is attending: ", 0, 5);
return static_cast<college>(anInt);
}//inputCollege
string collegeToString (college aCollege) {
string returnString;
if (aCollege == LPC)
returnString = "LPC";
if (aCollege == SFO)
returnString = "SFO";
if (aCollege == BRK)
returnString = "BRK";
if (aCollege == SLU)
returnString = "SLU";
if (aCollege == WASHU)
returnString = "WASHU";
if (aCollege == CSC)
returnString = "CSC";
return returnString;
}//collegeToString
string majorToString (degreeMajor aMajor) {
string returnString;
if (aMajor == CS)
returnString = "CS";
if (aMajor == CIS)
returnString = "CIS";
if (aMajor == MTH)
returnString = "MTH";
if (aMajor == MUS)
returnString = "MUS";
if (aMajor == BUIS)
returnString = "BUIS";
if (aMajor == VIT)
returnString = "VIT";
if (aMajor == PHY)
returnString = "PHY";
if (aMajor == ASTR)
returnString = "ASTR";
if (aMajor == ENG)
returnString = "ENG";
return returnString;
}//majorToString
void writeStudents(vector<student> &mystudentVector, student myStudent, Cstudent myCStudent, fstream &yourFile) {
char userInput;
yourFile.open("students.bin", ios::in | ios:: binary);
if (yourFile.fail()) {
yourFile.close();
}//if
else {
cout << "File already exists. Would you like to overwrite it?" << endl;
cout << "<Y>es, <N>o ==> ";
while (true) {
cin >> userInput;
if (userInput == 'Y' || userInput == 'y') {
yourFile.close();
yourFile.open("students.bin", ios::out | ios:: binary);
for (unsigned int i = 0; i < mystudentVector.size(); i++) {
strcpy(myCStudent.fname, mystudentVector[i].fname.c_str());
strcpy(myCStudent.lname, mystudentVector[i].lname.c_str());
yourFile.write(reinterpret_cast<char *> (&myCStudent), sizeof(myCStudent));
}//for
yourFile.close();
break;
}//if
else if (userInput == 'N' || userInput == 'n') {
cout << "Not writing the file." << endl;
yourFile.close();
break;
}//else if
else {
cout << "Invalid selection, please try again." << endl;
}//else
}//while
}//else
}//writeBoats
void readStudents(vector<student> &mystudentVector, student myStudent, Cstudent myCStudent, fstream &yourFile) {
string aString;
char aCString[54];
yourFile.open("students.bin", ios::in | ios::binary);
if (yourFile.fail()) {
cout << "File does not exist, try again later.\n";
yourFile.close();
}//if
else {
yourFile.open("students.bin", ios::out | ios::binary);
cout << "Open successful, reading students from students.bin" << endl;
for (unsigned int i = 0; i < mystudentVector.size(); i++) {
yourFile.read(reinterpret_cast<char *>(&myCStudent), sizeof(&myCStudent));
cout << "done copying" << endl;
if(yourFile.eof()) {
aString = aCString;
mystudentVector.push_back(myStudent);
break;
}//if
}//for
yourFile.close();
}//else
}//readStudents
#endif /* STUDENT_HPP_ */
Last edited on Dec 14, 2015 at 4:04am UTC