Advertisement

Files and null bytes...

Started by December 04, 2000 09:57 PM
3 comments, last by Corfe 24 years, 1 month ago
I have been messing with a map editor, and I decided to cut down on size (the map is a height map with float''s and several other float variables), I am converting it to binary.... binary just like it is stored in memory but on a file, which should theoretically work I would think - problem is a few functions seem to have issues reading null bytes (0x00, ''\0'', whatever you wanna call it).... and this completely ruins the whole idea... what is the best way to read a byte from a function, and get the answer even if it''s null AND not cause any other problems, such as becoming fixed on that null byte... any people here got ideas? I tried both ifstream and FILE*, but both seem to be pretty much the same when it comes down to it, and neither could seem to solve my problem... maybe the error is elsewhere in the code, but I''m just wondering if my lead is correct... do either of these file input methods have issues reading null bytes? (I''m reading in character-by-character manually, not using strings or anything like that). Thanks in advance to whoever may help
Sure if you''re going to use fscanf on binary data, you''re asking for trouble, but otherwise I''ve had no problems using fread or fgetc on binary data including data that had NULL values. What exactly is the nature of your problem?
Advertisement
The get() function should be able to read in a ''\0'' and it''s called a terminating char, not a null byte. It still occupys 1 byte.

Take a look at flipcode. I have written a binary IO class wrapper that may save you headaches.
quote: Original post by Corfe

I have been messing with a map editor, and I decided to cut down on size (the map is a height map with float''s and several other float variables), I am converting it to binary.... binary just like it is stored in memory but on a file, which should theoretically work I would think - problem is a few functions seem to have issues reading null bytes (0x00, ''\0'', whatever you wanna call it).... and this completely ruins the whole idea... what is the best way to read a byte from a function, and get the answer even if it''s null AND not cause any other problems, such as becoming fixed on that null byte...

any people here got ideas?

I tried both ifstream and FILE*, but both seem to be pretty much the same when it comes down to it, and neither could seem to solve my problem... maybe the error is elsewhere in the code, but I''m just wondering if my lead is correct... do either of these file input methods have issues reading null bytes? (I''m reading in character-by-character manually, not using strings or anything like that).

Thanks in advance to whoever may help


You have to make sure you open the file with the "b" option. like fopen("file.dat", "rb");.

Thanks.... yeah I know it still takes up a byte, I was just referring to a 0x00 byte... I think I will look into Void''s binary IO class, it looks good... thanks people!

This topic is closed to new replies.

Advertisement