hello everybody
i'm trying to write/read some simple binary files in my aplication. i'm able to do it in text files, but binary is being a pain.
i have some ints, bools and enums that i want to write to a file and read later.
but according to the reference on this site, the function write() asks for a "pointer to char". how am i supposed to do that with an int?
This example creates a file named "example.dat", stores some integers in it, then reads them back.
The issue of the read/write function requiring a "pointer to char" is dealt with by means of a cast, where the pointer to the data is reinterpreted as a pointer to a character. I've used two different styles of cast here, because you are likely to come across both.
I also used the sizeof() operator in two different ways, with either the name of the object, or the name of the type.
but if i use this approach, won't i have the problem pointed in the article (int might be 8 bytes on one machine, but only 4 bytes on another) at the time of reading?
i'd like to know where does he take u16 and u32 from. i don't see a definition of that..
i made the multiplications just to make sure it's everything right :)
now PLEASE, if i did something wrong or could do better, please let me know.
thanks Chervil and Disch, you helped me a lot!
That seems to be mostly along the right lines. Though it does (or did) contain an error which caused an access violation when I tried to run it. for(size_t i=0; i<=total_values; i++)
(the condition should be i<total_values - I think you edited the code to fix this.
I wasn't entirely sure why you were using a vector at all, since you could create the file just as well without it. Also, when printing the value of block you may have intended to print the address rather than the default behaviour which is to treat it as a character string.
Here's my version, which is only very slightly different, and any changes I've made are 99% down to personal preference.
yeah, i edited that error :P
and thanks, i really intended to print the adresses.
just one more thing: how can i convert from bool to char using the same approach? i mean, a fixed amount of memory. do i have to convert it to uint16_t (or maybe uint8_t?) and then convert it again to char?
would it be something like
error: taking address of temporary [-fpermissive]|
i don't get it. is it really temporary? if so, why don't the line 9 returns the same error?
thanks!
EDIT:
after a while i discovered: i can't use vector<bool> for this, as the vector stores each bool in a single bite, instead of a byte. so, i changed to std::deque <bool> bool_values = {true, true, false, true};
I'm not sure why the two cases behave differently. I tried this in two different compilers, one worked ok, the other gave this same error. I can guess at some reasons, but that isn't really helpful.
Meanwhile, if you are really sure that the data item occupies just a single byte, then you could replace this:
The storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
but thank you for your help! now it's working perfect! i'm saving chars, bools, ints and some enums to binary file and sucessfully loading everything :)
oh no, sorry, i was talking about your other post.
you're right, i changed it when trying to discover what was causing the error in bools vector. i fixed it, thanks for the note :)
Out of interest, I was looking at two other aspects of this. One, reading and writing the entire contents of the vector in a single instruction. Secondly, handling data of a variable length.
There may be flaws in this code, but i think its heading in the right direction
sorry for bumping this thread, but i have another question regarding this: how can i safely store floats and doubles? there is nothing like "ufloat16_t" (:P) so, how to store values that will be correctly read from different systems?