May 15, 2014 at 7:03am UTC
Hello! My problem today involves reading and saving a class object from/to a file (file name provided by user). I have the program reading and writing to the file, however the output is saving somewhat incorrectly, causing the next iteration of the program to run incorrectly as well.
I am trying to place the data into the file as follows:
1 2 3 4 5 6 7 8 9 10
Last Name
First Name
Middle Name
Street
City
State
Zip
ID #
Phone #
EOF (after last student record has been saved)
What I am getting is this:
1 2 3 4 5 6 7 8 9 10
Middle
First
Last
Street
City
Zip
ID #
Phone #
EOF
--Here is the relevant code to my problem, more can be provided if necessary, however I have included all that is required to fix this problem (I think). Thank you for your time!
Here is my class definition: (large parts excluded as not necessary to solve problem)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#define NUMBER_OF_GRADES (5)
#define ID_SIZE (9)
class Student
{
public :
ostream &DisplayFile (ostream & = cout) const ;
ostream &DisplayScr (ostream & = cout) const ;
istream &Read (istream & = cin);
bool SaveToFile (fstream &);
bool ReadFromFile (fstream &);
private :
FixedDigit <ID_SIZE> ID;
PhoneNumber Phone;
Address Addy;
Name StudentName;
};
Here are my class defined save and read functions:
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
bool Student::ReadFromFile (fstream & in)
{
if (in.is_open ())
{
StudentName.Read(in);
if (StudentName.GetFirst() == "EOF" )
return true ;
Addy.Read (in);
ID.Read (in);
Phone.Read (in);
return true ;
}
else
return false ;
}
bool Student::SaveToFile (fstream & out)
{
if (!out.is_open())
throw FileSaveError;
else
return DisplayFile (out);
}
ostream & Student::DisplayFile (ostream & out) const
{
out << GetFirstName() << '\n'
<< GetMiddleName() << '\n'
<< GetLastName() << '\n'
<< GetStreet() << '\n'
<< GetCity() << '\n'
<< GetState() << '\n'
<< GetZip() << '\n'
<< GetID() << '\n'
<< Phone.GetAreaCode() << '\n'
<< Phone.GetPhoneNumber() << '\n' ;
return out;
}
istream & Student::Read (istream & in)
{
StudentName.Read(in);
Addy.Read (in);
ID.Read (in);
Phone.Read (in);
return in;
}
Here are my save functions to save my array of 'Student' objects:
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
#include "Student.h"
bool StudentsSave (const Array <Student, LOWER, UPPER> & StudentsArr, const WCS_String & filename, int NumStudents)
{
int SaveCount = 0;
Student pStu;
fstream out;
out.open (filename, ofstream::out);
if (!out.is_open())
return false ;
for (int i = LOWER; i < (LOWER + NumStudents); i++)
{
pStu = StudentsArr[i];
pStu.SaveToFile(out);
SaveCount++;
}
cout << "//Saved " << SaveCount << " student records successfully.\n" ;
out.write ("EOF" , (sizeof (char ) *3));
out.close();
return true ;
}
bool StudentsRead (Array <Student, LOWER, UPPER> & StudentsArr, const WCS_String & filename, int & NumStudents)
{
fstream in;
in.open (filename, ifstream::in);
if (!in.is_open())
return false ;
NumStudents = -1; //Offset the EOF increment
for (int i = LOWER; i < UPPER; i++)
{
StudentsArr[i].ReadFromFile(in);
NumStudents++;
if (StudentsArr[i].GetFirstName() == "EOF" )
break ;
}
in.close();
return true ;
}
**Sorry for the long post :)
Last edited on May 15, 2014 at 7:03am UTC
May 15, 2014 at 7:41am UTC
What I am getting is this:
Is this what you get in output file or what you get when you are trying to read it again?
Last edited on May 15, 2014 at 7:41am UTC
May 15, 2014 at 7:45am UTC
When I personally open the file after the 1st save operation (not the program), I get this:
Taylor (Middle name)
Paul (Last name)
Jeremy (First name)
Warren (St.)
Frisco (City)
Texas (State)
75034 (Zip)
111111111 (ID)
123 (Area Code)
123123 (Phone #)
So my program is switching the names around in the ostream, and also not putting an EOF marker in.
This makes it impossible for me to read into my class
Last edited on May 15, 2014 at 7:47am UTC
May 15, 2014 at 7:54am UTC
Does GetFirstName()
etc. functions work correctly?
Does functions which read names (when creating students) work correctly?
Do you save your file by calling StudentsSave function and not by calling .SaveToFile()
manually?
May 15, 2014 at 8:06am UTC
I fixed my problem x_x
It was all caused by an issue with a class constructor accepting my data in a different order than intended.