Length of a file

Previous topic - Next topic

Moru

How do I know if I am about to read past the end of a file? I want to read a whole chunk of data to skip to the next chunk in the file but what if the file is damaged and it ends after 10 bytes and I try to read 20 bytes? GL Basic just crashes with a not very helpful error message, how can I prevent this happening?

I'm doing something similar to this:
Code (glbasic) Select
WHILE temp$ <> "BODY"
IF length > 0
READSTR filenr, temp$, length
ENDIF
READSTR filenr, temp$, 4
READLONG filenr, length
WEND
I would like a command for checking what byte I'm reading from in the file (file pointer) and a way of checking how long a file is in bytes.

Kitty Hello

See manual for "OPENFILE":
ok# = ENDOFFILE(channel#)

Moru

will that work also if I*m about to skip a chunk of data that the file says should be 10 000 bytes but the file is damaged and only contains 50 bytes? How does the code know that next read will be 10 000 bytes?

Kitty Hello

ah. You want to skip data? That's not possible so far. Use READLONG to read dummy bytes for the contents.
Also - don't you know the file format and length when reading?

Moru

Yes, I will do it that way instead, the second long contains the length of the file in motorola style byte order and then count the number of skips I do. I'm skipping by just reading a string that is X bytes long. Works fine so far except when it reaches that block that is 0xC8 bytes in length wich ends up with a negative value. I guess I will have to skip with just a loop reading bytes and check for eof but that will make it realy slow to read the maps I guess.