Hello everyone,
I've written (VC++ 2008 Express Edition) a simple database operating on txt file, but i have problem with retrieving int objects from it. I hope i won't need to paste my whole code here, anyway i assume the rest is OK.
This is my class:
1 2 3 4 5 6 7 8 9
|
class Student
{
private:
char* Name;
char* Surname;
int Age;
.........etc
};
|
And those are functions for saving/loading data (from) file:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void SaveToFile(Student* student, int recordNumber)
{
fstream nfile("database.txt",ios::out);
for (int i=0; i<recordNumber; i++)
{
nfile<<student[i].PrintName()<<endl;
//Printxxx() functions are returning Name/Surname/Age...
nfile<<student[i].PrintSurname()<<endl;
nfile<<student[i].PrintAge()<<endl;
}
nfile.close();
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void LoadFromFile(Student* student, unsigned &Record)
{
int i = 0;
ifstream nfile("database.txt",ios::in);
if(nfile.good())
{
while (!nfile.eof()) {
nfile>>student[i].PrintName()<<endl;
nfile>>student[i].PrintSurname()<<endl;
nfile>>student[i].PrintAge()<<endl; //(1)look below
Record++;
i++;
}
}else
cout<<"Error while opening the file"<<endl;
nfile.close();
}
|
Ad. (1) - i always receive an error in this part:
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Could not find any suitable info elsewhere, what i found were infos about streamstring and operator overload(but i ain't sure if that may help here).
Does anyone know how that part of code should look like?
Ad. 2 - If i throw away those 2 lines, responsible for saving/loading Age (that nasty integer) the program works fine, but always when i save the data and then after a while load it from the file i receive an extra empty field...
I'm not sure why it happens. In the begin i thought EOF is equal to "0" but that's not true... Any ideas?
I hope my poct wasn't that chaotic. I'm looking forward you will find a proper solution for me...
Thx in advance
John Kravetzki