I don't need the file to be human readable, but I do need the program to be able to read it and display the information there in human readable form. Right now, when I use the readfromfile function that I wrote the program returns the name as the correct name followed by a bunch of random characters and the integers are always the same.
I will include both functions, in case the problem is the reading function.
Thanks!
Code:
void plant::readfromfile(int pnum, string filename){
//cout << "Got to read from file function." << endl;
fstream fbin(filename.c_str(), ios::binary | ios::in | ios::out);
if(!fbin){
cout << "Error opening fbin";
}
//cout << "Opened file." << endl;
int recsize = sizeof(int) + (sizeof(int) + sizeof(int) + sizeof(int))*2 + sizeof(string);
fbin.seekp(pnum * recsize);
//cout << "Found place in file." << endl;
fbin >> name;
//fbin.read(reinterpret_cast<char*>(&name), sizeof(string)); //<-- old version
//cout << "Read string from file." << endl;
int m, d, y;
fbin.read(reinterpret_cast<char*>(&m), sizeof(int));
fbin.read(reinterpret_cast<char*>(&d), sizeof(int));
fbin.read(reinterpret_cast<char*>(&y), sizeof(int));
waterd.set(m,d,y);
fbin.read(reinterpret_cast<char*>(&m), sizeof(int));
fbin.read(reinterpret_cast<char*>(&d), sizeof(int));
fbin.read(reinterpret_cast<char*>(&y), sizeof(int));
aquired.set(m,d,y);
//cout << "Read date info from file." << endl;
fbin.read(reinterpret_cast<char*>(&size), sizeof(int));
//cout << "Finished reading everything from file." << endl;
fbin.close();
//cout << "Leaving function." << endl;
return;
}
Code:
void plant::writetofile(int pnum, string filename){
//cout << "Got to write to file function." << endl;
fstream fbin(filename.c_str(), ios::binary | ios::in | ios::out);
if(!fbin){
cout << "Error opening fbin";
}
//cout << "Opened file." << endl;
int recsize = sizeof(int) + (sizeof(int) + sizeof(int) + sizeof(int))*2 + sizeof(string);
fbin.seekp(pnum * recsize);
//cout << "Found place in file." << endl;
fbin << name; //<-- new version
//fbin.write(reinterpret_cast<char*>(&name), sizeof(string)); //<-- old version
//cout << "Wrote string to file." << endl;
int x;
x=waterd.getm();
fbin.write(reinterpret_cast<char*>(&x), sizeof(int));
x=waterd.getd();
fbin.write(reinterpret_cast<char*>(&x), sizeof(int));
x=waterd.gety();
fbin.write(reinterpret_cast<char*>(&x), sizeof(int));
x=aquired.getm();
fbin.write(reinterpret_cast<char*>(&x), sizeof(int));
x=aquired.getd();
fbin.write(reinterpret_cast<char*>(&x), sizeof(int));
x=aquired.gety();
fbin.write(reinterpret_cast<char*>(&x), sizeof(int));
//cout << "Wrote date info to file." << endl;
fbin.write(reinterpret_cast<char*>(&size), sizeof(int));
//cout << "Finished writing everything to file" << endl;
fbin.close();
//cout << "Leaving function" << endl;
return;
}