Reading objects till eof

Hi,
I try to read all the objects contained in a file with the following code:

1
2
3
4
5
6
7
8
9
10
11
vector<Cube>cubes();

...

ifstream in(file, ios::binary);
if(in)
{
	for(int i = 0; !in.eof() ; i++)
	in.read((char *)&cubes[i], sizeof(cubes[i]));
	in.close();
}


But the g++ compiler tells me this:
warning: pointer to a function used in arithmetic [-Wpointer-arith]

and
warning: invalid application of ���sizeof��� to a function type [-Wpointer-arith]

Is there another way to read objects into the vector from the file till the enter of the file is reached?
Last edited on
Your error messages (pointer arithmetic on a function and application of sizeof to a function) are because "cubes" in your code sample is a function, not a vector.

1
2
3
4
int f(); // function taking nothing and returning int
vector<Cube>cubes(); // function taking nothing and returning vector<Cube>
int n; // number of type int
vector<Cube>cubes; // vector of type vector<Cube> 


As for reading up to end of file, almost every input function, including istream::read(), has a return value that you can use.

1
2
3
vector<Cube> cubes;
for(Cube c; in.read( (char*)&c, sizeof c); )
    cubes.push_back(c);
Last edited on
Dooh!
:genius:
Thanks and sorry for the ignorance!
Topic archived. No new replies allowed.