raffamaiden <> writes:
> First off, thanks for the answers.
>
>> Â*You have to define your file format. They are severals...
>> Â*You can choose text-oriented, or binary ones.
>> Â*There is no 'best' solution. It depends on you needs.
>
> I want to use a binary file format because i feel the final file will
> be smaller and doesn't require atoi() and other stuff to retrieve the
> data.
If you use a binary file format, either you'll have to define the
exact format (and translate to and from that format when accessing
the file), or you'll have to give up on being able to read the file
on other systems. Straight binary (fwrite'ing structs directly,
for example) can make sense for files that will be used *only* by the
same program on the same system.
Text is far more portable, and you may find that the space and time
overhead of using text rather than binary isn't that much of an issue.
> An integer should be stored as 32-bit.
Why? I'm not saying you're wrong, but why 32 bits in particular?
See <stdint.h> for definitions of types of particular sizes. uint32_t
might be the best thing for your purposes, at least if you don't need
negative values.
>>Serialize your data... e.g.
>>If you know you're using 32-bits of the data
>>
>>unsigned char buf[4];
>>for (int x = 0; x < 4; x++) {
>> buf[0] = val & 0xFF;
>> val >>= 8;
>>}
>>
>>outlen = fwrite(buf, 1, 4, outfile);
>>
>>Of course smarter would be to write a function to store 32-bit ints to
>>a FILE then just call it when needed...
>
> I like this solution, but I have few more questions: Is 'char'
> guaranteed to be exactly 1 byte by the standard? Because if not that
> would not make sense.
As others have said, C defines a "byte" as the size of a char object,
which is *at least* 8 bits. (You'll see the word "byte" with other
meanings in other contexts.) If you're not dealing with DSPs and
embedded systems, you can probably get away with assuming that a byte is
8 bits -- but I suggest making the assumption explicit:
#include <limits.h>
#if CHAR_BIT != 8
#error "CHAR_BIT != 8"
#endif
/* Now we can safely assume that bytes are 8 bits.
> Also another question: in your example my integer, for which i use
> only the last 32-bits whatever its size in memory is, would be in the
> variable "val". How about encoding? Does the C standard (let it be
> C90) specify how an integer should be encoded in memory, or not? If
> not, suppose i'm running my program in two implementations A and B. A
> use two's complement encoding, while B use sign and magnitude. So if A
> save the integer with the above c code, it will save exactly 32 bits,
> but B will recognize another number because it use another encoding in
> the same 32-bits.
C permits signed integers to be stored in 2's-complement,
1s'-complement, or sign-and-magnitude. (That's C99; C90 was
less specific, but I don't think you'll find an implementation
that uses anything else.) The vast majority of modern systems
use 2's-complement. but if you only write unsigned values to
files you can avoid that. If you need negative integers, you can
either define your own file format or just assume a 2's-complement
representation; the latter is less portable, but unlikely to be a
problem in practice.
Byte order is another issue; google "endianness" for more
information. POSIX provides byte-order conversion functions
(htonl et al); depending on POSIX further reduces portability,
but not drastically so.
[...]
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"