well, i was assigned my first project....but it's rather dull, so i decided to go WAY overboard on it.....
i created a random access file to store events for a calendar program..events contains the date and a description of it...
i'll try to post a little code as i can....i'm not asking you to find the bug...am i asking how i would find the bug...the biggest problem i'm facing is that i can't read the file (as a human)....
the output and description of the error is below the code.
Code:
int FileManager::addRecord(Record &srcRec)
{
srand(56); //really out of place, just haven't decided where
//to put it yet..i should probably use the time too
srcRec.setKey(uniqueKey());
int openRecord = firstAvailable();
ofstream fout(filename, ios::binary);
fout.seekp(openRecord * sizeof(Record));
fout.write(reinterpret_cast<const char *>(&srcRec),
sizeof(Record));
fout.close();
}
// i'm not planning on keeping this function, it's just for testing
void FileManager::print(void)
{
ifstream fin;
fin.open(filename, ios::binary);
while(fin && !fin.eof())
{
fin.read(reinterpret_cast<char *>(&cur_rec),
sizeof(Record));
cout << endl << endl;
cout << "Key: " << cur_rec.getKey() << endl;
cout << "Date: " << cur_rec.getMonth()<<"/"<<
cur_rec.getDay()<<"/"<< cur_rec.getYear() << endl;
cout << "Label: " << cur_rec.getLabel() << endl;
cout << "Desc: " << cur_rec.getDesc() << endl;
}
fin.close();
}
Code:
the only record i added was the one with the key 221....
and for some reason it prints twice...i can't tell if this
is a looping error in the print, or in the write...
Key: 0
Date: 0/0/0
Label:
Desc:
Key: 221
Date: 1/13/1999
Label: xxxx
Desc: nnnn
Key: 221
Date: 1/13/1999
Label: xxxx
Desc: nnnn