jeudi 15 août 2019

Custom binary file triggers failbit in c++

tl;dr: What are necessary control characters that custom file must have in order not to trigger [or trigger] badbit, failbit, badbit, eofbit dependant on OS?


I am working with Cygwin and Notepad++, using Windows 7 on an x64 laptop.

I have an file called genesis.o that I typed in hex editor that is in same directory as sandbox.cpp from which I am doing reading/writing. The file "genesis.o" contains:

genesis.o 
-----------------
58 - 4f - 58 - 4f 
58 - 58 - 58 - 58 
3f - 3f - 3f - 3f 
00 - 00 - 00 - 3f  
00 - 00 - 3f - 00
00 - 3f - 00 - 00
3f - 00 - 00 - 00 
00 - 00 - 00 - 58
00 - 00 - 58 - 00
00 - 58 - 00 - 00  
58 - 00 - 00 - 00 
48 - 45 - 4c - 4c  

So far, I have read and manipulated custom structures without any regard of validity of the file. I used stat to check if file is accessible and that was enough.

This file (with and without extension ".o") passes all check ups I made except file.rdstate in which it always returns 4 (std::ios::failbit).

Error doesn't show itself on any other normal file, so I am guessing that some sort of control character sequence before/after or in file actually tells the std::fstream that file is valid.

Since no other file (except those typed in a hex editor) triggers this behaviour, is there a way to structure an custom binary file to be recognised by fstream? Some sort of control characters, preset flags etc.?

I am using std::ios:in | std::ios:binary. I am reading it by getting stat buffer.st_size -> divide it by 4 (since I read 4 byte integers) and:

uitn32_t temp = 0;
file.read( (char *)(&temp), sizeof(uint32_t) );  

It is notable to mention, that I can read that binary file even if file.rdstate returns an failbit.

Aucun commentaire:

Enregistrer un commentaire