Problem with retrieving data from file

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
Last edited on
please use code [tags], it makes it a lot easier on us to read and you will get greater responses
1. I'm assuming PrintAge() returns an int. The problem with the assignment is that you're trying to assign to a temporary value returned by PrintAge(), which is invalid. To change the least amount of code, you could make it return an int &, but this is basically the same as making Age public.
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?


wow, what kind of noob i am :) I forgot what means <<endl :))))
here is my solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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...
        if(i==(recordNumber-1)
        {
	       nfile<<student[i].PrintSurname();
        }else
               nfile<<student[i].PrintSurname()<<endl;

	//nfile<<student[i].PrintAge()<<endl;

}
nfile.close();
}


nfile>>student[i].PrintAge()<<endl; //(1)look below
Don't use << or endl when performing input.

(Not unless, of course, you do something like I did here: http://www.cplusplus.com/forum/beginner/11402/page1.html#msg53942 , and define your own endl manipulator for input streams...)
i think i found the solution (and i was wrong, it is possible to overload the operator while having string/int variables in the class)
Here it is in complete (for other generations :P):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Student
{
private:

std::string Name;
std::string Surname;
int Age;

public:

friend std::ostream& operator<<(std::ostream& nFile, Student& student);
friend std::istream& operator>>(std::istream& nFile, Student& student);
.....
etc


1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::ostream& operator<<(std::ostream& nFile, Student& student)
{
	nFile.write(reinterpret_cast<char*>(&student.Age), sizeof(student.Age));
	nFile << student.Name << '\0';
	nFile << student.Surname << '\0';
	return nFile;
}
std::istream& operator>>(std::istream& nFile, Student& student)
{
	nFile.read(reinterpret_cast<char*>(&student.Age), sizeof(student.Age));
	std::getline(nFile, student.Name, '\0');
	std::getline(nFile, student.Surname, '\0');
	return nFile;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void SaveToFile(Student* student, int recordNumber)
{
std::fstream nfile("database.txt",std::ios::out);
for (int i=0; i<recordNumber; i++)
    nFile << student[i];
nFile.close();
}

void LoadFromFile(Student* student, unsigned &Record)
{
int i = 0;
std::fstream nfile("database.txt",std::ios::in);
if(nfile.good())
{
	while (nFile >> student[i]) {
		Record++;		
		i++;
	}
}else
	cout<<"Error while opening the file"<<endl;
nFile.close();
}
Last edited on
Topic archived. No new replies allowed.