G
Glen
Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??
thanx
glen
is it using fwrite??
thanx
glen
Glen said:Is it possible to write a structure to a file in c...as in c++...??
Yes.
is it using fwrite??
Mike said:That's what you'd use to write an exact binary
image, yes. Another way would be to write
a textual version, one member at a time.
Rogério Brito said:The second option would be better, because, AFAIK,
an exact binary image may have problems
with the size of the members of the structure,
with the alignment chosen by the compiler
for the members of the structure and even byte-endianness,
if the output file is to be used across different platforms.
I may be wrong, thought, and I would love to be corrected here if I am.
In said:Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??
a textual version, one member at a time.
-Mike
want...Glen said:well i did it in textual ...well fwrite isnt working..err in the way i
Glen said:well i did it in textual ...well fwrite isnt working..err in the way i want...
In said:The second option would be better, because, AFAIK an exact binary image
may have problems with the size of the members of the structure, with
the alignment choosen by the compiler for the members of the structure
and even byte-endianness, if the output file is to be used across
different platforms.
E. Robert Tisdale said:The "textual version" may have problems with precise representation
of floating-point numbers.
[...]In <[email protected]>
There is more than one way of doing it. The two most popular approaches
are:
1. Use a text file, and convert each field of the structure to a textual
representation, using fprintf.
2. Use a binary file and dump the binary representation of the
structure value with fwrite.
The first approach is more portable (the value can be read on a different
platform, or even with a program written in a different language), but
uses more disk space and CPU cycles.
The second approach uses less disk space and CPU cycles, but the resulting
binary file can be read only by a C program compiled with the same
compiler, on the same platform.
is it using fwrite??
I used Google
http://www.google.com/
to search for
+"C++" +"serialization"
and I found lots of stuff.
Barry said:You actually search on C++ to find stuff about C?
In said:[...][email protected] (Dan Pop) said:In <[email protected]>
There is more than one way of doing it. The two most popular approaches
are:
1. Use a text file, and convert each field of the structure to a textual
representation, using fprintf.
2. Use a binary file and dump the binary representation of the
structure value with fwrite.
The first approach is more portable (the value can be read on a different
platform, or even with a program written in a different language), but
uses more disk space and CPU cycles.
The second approach uses less disk space and CPU cycles, but the resulting
binary file can be read only by a C program compiled with the same
compiler, on the same platform.
You're not guaranteed that the binary file can be read by a C program
compiled with a different compiler or on a different platform, but
*sometimes* it can be, if you're very careful and/or very lucky.
For different compilers on the same platform, compiler writers
typically use the same layout algorithms to allow data portability.
(This is not guaranteed; it's something you need to verify.)
[...][email protected] (Dan Pop) said:In <[email protected]>
(e-mail address removed) (Glen) writes:
Is it possible to write a structure to a file in c...as in
c++...?? is it using fwrite??
There is more than one way of doing it. The two most popular
approaches are:
1. Use a text file, and convert each field of the structure to a
textual representation, using fprintf.
2. Use a binary file and dump the binary representation of the
structure value with fwrite.
The first approach is more portable (the value can be read on a
different platform, or even with a program written in a different
language), but uses more disk space and CPU cycles.
The second approach uses less disk space and CPU cycles, but the
resulting binary file can be read only by a C program compiled
with the same compiler, on the same platform.
You're not guaranteed that the binary file can be read by a C program
compiled with a different compiler or on a different platform, but
*sometimes* it can be, if you're very careful and/or very lucky.
For different compilers on the same platform, compiler writers
typically use the same layout algorithms to allow data portability.
(This is not guaranteed; it's something you need to verify.)
You can do *anything* you want, if, instead of relying on the language
specification, you rely on your own checking that it works in a
particular set of circumstances, so I fail to see your point.
If you want to write code that works *by design*, you MUST follow my
guidelines above, period.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.