Advertisement

File I/O

Started by February 11, 2001 08:01 PM
1 comment, last by The Raven 23 years, 11 months ago
Hi, I''m trying to read a height map to a binary file, and there seems to be a large problem, half way through reading the file it seems to think is the end. it happens at x = 11, y = 31. This is the code that writes the file, I check the data with the debugger and it hold the correct value at this time. #define WRITE(x) write((char *)&x, sizeof(x)); ofstream fout("output.map"); fout.WRITE(XPixels); fout.WRITE(YPixels); for(x = 0; x < XPixels; x++) { for(y = 0; y < YPixels; y++) { fout.WRITE(VertexData[x][y]); } } for(x = 0; x < XPixels; x++) { for(y = 0; y < YPixels; y++) { fout.WRITE(VertexNormals[x][y]); } } fout.close(); And this is the code that reads it in...in another program. #define READ(x) read((char *)&x, sizeof(x)) ifstream fin("Data\\Maps\\output.txt"); fin.READ(XAmount); fin.READ(YAmount); VertexData = new vec3_t*[XAmount]; VertexNormals = new vec3_t*[XAmount]; for(x = 0; x < XAmount; x++) { VertexData[x] = new vec3_t[YAmount]; VertexNormals[x] = new vec3_t[YAmount]; } for(x = 0; x < XAmount; x++) { for(y = 0; y < YAmount; y++) { fin.READ(VertexData[x][y]); if(fin.eof()) exit(0); } } for(x = 0; x < XAmount; x++) { for(y = 0; y < YAmount; y++) { fin.READ(VertexNormals[x][y]); } } fin.close(); Any help would be much appreciated
You are probably hitting the actual EOF character. since you are using a binary file, you shouldn''t use it. Instead, write out the size of your file at the very beginning, then you will know how much to read, and not have to test for the end. i believe read will return some sort of error code if it fails, and you can test for that.
Advertisement
When writing try using: fout("output.map", ios_base::out | ios_base::binary);

For reading: fout("output.map", ios_base::in | ios_base::binary);

It''s been a while since I looked at stl file i/o. My syntax may be slightly off. Check your docs.

-Mike
-Mike

This topic is closed to new replies.

Advertisement