Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Low level file read problems

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
I wrote a function that would read values out of a binary file. It works great for awhile...after reaching a seemingly random point in the file, it just stops working. All subsequent calls will fail until I close the file and reopen it.

Here is my code. "myfile" is a global that is assigned a file handle in a previous function call. _Error is an API call to generate an error message in Visual FoxPro:
[tt]
double getdouble(void)
{
if (myfile==NULL) // No file is open
{
_Error(113);
return (double)0;
}
double i;
if (fread(&i,sizeof(double),1,myfile)>0)
// read successful
return i;
else // Error reading file
_Error(104);
return (double)0;
}
[/tt]
After calling this routine a few times and working my way into the file, the fread() simply stops getting data. It happens in the exact same place in a given file, but different places in different files. I'm pretty sure I haven't gone past the EOF since the data that has been retrieved up to this point is still near the beginning.

Any ideas what could cause this?

Ian
 
Well the only thing I can see wrong is that you are returning a double that will effectivly go out of scope when the function ends.

The double i only has a life span while this function is being called. I'd suggest that you have the function accect a double* and read into that.

HTH.
William
Software Engineer
ICQ No. 56047340
 
No; my double value works properly. It is SUPPOSED to go out of scope. It's the return value of the function.

My problem is that the "fread" function will simply stop returning anything until I close the file and reopen it.

In case it matters:

I'm running Windows 2000 with SP1, Visual C++ 6.0 with SP5.

Ian
 
Of course it does. You know there are times that I really wonder why I get up in the mornings, and how stoopid one can be as well!

I assume that the format of the file you are reading is in fact what you are expecting, and you're not reading data that can be interpreted as EOF?

Just a thought.


William
Software Engineer
ICQ No. 56047340
 
Yes, I know the format of the file. Each file contains hundreds or thousands of DOUBLE values representing a lattitude and longitude on the earth's surface. The most I've been able to read in so far is 25. If I close the file and open it again I can continue, but I would have to completely rewrite my input function to keep track of the current file position.

I felt no need to do this originally since the file is read sequentially, beginning to end, and the records are variable-length. It would be a SERIOUS pain to have to keep track of the current file position, watch for errors, and close/reopen the file when a read error occurs.

Any other suggestions? I'd rather fix the problem than work around it. Besides, closing and re-opening the file will add significant time to the file access.

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top