Re: Low level file access

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 19/04/13 20:11, ballsystemlord wrote:
> I'm a beginner am and wondering how to open a binary file, read a certain
> amount of bytes from it and then write the data after proccesssing it into a
> different file. I would like to use several threads and to make the data
> transferable from on computer to another.
> I've read the docs and am still unsure how to make my threads read the data
> and replace it in the same order that they took it up from the original. 
> Further I have no idea how to write down part of an int.
First, this isn't a doubt about gcc, but about programming (gcc can compile
different languages, as you didn't specify, you are probably interested
in doing
it in C).
Second, there's no need to use several threads for this. You won't gain
anything.
You could annotate each thread with the piece of the file they should
read and then
write, but a single-thread program will work equally well, while being
simpler.

Third, making the data transferable from one computer to another. An
option would
be to store the data as text. As you said you want to work with a binary
file,
you are probably storing the variables/structure directly into the file.
Some
problems you may encounter:
- Different type widths. You write a long in one machine where it has 8
bytes, then
read it from another where long has 4 bytes. I'd use for the data to be
stoed the
int*_t types from <stdint.h> to have a fixed width.
- Different type alignment. If the alignment of your struct isn't
optimal, the compiler
will add some padding to fix it. This is not an immediate problem, but
if you try to
use the same code with a different compiler (which uses slightly
different rules) or
another version of the same compiler, where those rules have changed,
then there
will be a nasty mismatch. Solution: use packed structs.
- Endianness <http://en.wikipedia.org/wiki/Endianness>. You move it to a
computer using a different byte-order. Solution: convert
to a file-format endianness before storing in file, convert back when
reading. You can
use functions like htonl()
You can use a framework like XDR
<http://en.wikipedia.org/wiki/External_Data_Representation> for that,
but I'd start but understanding how to do
the things manually.

Finally, for your strange request on "how to write down part of an int",
you probably
want to work with the bytes (use the char type) of the int .

Cheers






[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux