Writing a structure

G

Glen

Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??
thanx
glen
 
M

Mike Wahler

Glen said:
Is it possible to write a structure to a file in c...as in c++...??
Yes.

is it using fwrite??

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.

-Mike
 
?

=?ISO-8859-1?Q?Rog=E9rio_Brito?=

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.

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.

I may be wrong, thought, and I would love to be corrected here if I am.
 
E

E. Robert Tisdale

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.

The "textual version" may have problems
with precise representation of floating-point numbers.

The range and precision of fixed (integral) and floating-point types
may vary from platform to platform
resulting in truncation or rounding
as quantities are read from a text file.
 
D

Dan Pop

In said:
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.

There are also compromises between the two methods, like XDR (a platform
independent binary data format).

Dan
 
G

Glen

Another way would be to write
a textual version, one member at a time.

-Mike

well i did it in textual ...well fwrite isnt working..err in the way i want...
 
M

Mike Wahler

Glen said:
well i did it in textual ...well fwrite isnt working..err in the way i
want...

Well, show us your code, and we'll help you fix it.
Also be sure to tell us what 'the way you want' is.

-Mike
 
E

E. Robert Tisdale

Glen said:
well i did it in textual ...well fwrite isnt working..err in the way i want...

Pointers and process id's won't make any sense
when you read the struct back into memory.

I used Google

http://www.google.com/

to search for

+"C++" +"serialization"

and I found lots of stuff.
 
D

Dan Pop

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.

OTOH, the output file might be supposed to be used *only* by the same
program, on the same platform.

To decide what option is better, one must know the program specification.

Dan
 
?

=?ISO-8859-1?Q?Rog=E9rio_Brito?=

E. Robert Tisdale said:
The "textual version" may have problems with precise representation
of floating-point numbers.

Indeed. I had not thought about the case of floating point numbers and
the problems that some libraries might have writing their (differing)
representation to files.

But since the "binary dump version" might also have problems with this
(since one can't guarantee that the systems where the file is written
and where the file is read use the same size and organization of
floating point numbers) in addition to those that I pointed above, I'd
still choose the "textual version" before going for the "binary dump"
version.

Of course, nothing beats a properly done, specialized serialization library.
 
K

Keith Thompson

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.)

It's not an approach I'd recommend, but I've worked on a system that
shares binary data files between VAX and Alpha, and on another that
shared binary data files between 68k and SPARC. In the former case,
we had to be careful to use the same floating-point formats on both
systems; it's not clear that the system could have met its performance
constraints if we had used a textual format. In the latter, I had to
insert dummy character array members to force common alignment; it was
ugly, but it worked.

This kind of thing is unlikely to work if the platforms have different
byte ordering.

But if you want to share binary data, be aware that you're going to be
spending a lot of your time keeping everything consistent, and
probably dealing with problems when things aren't consistent.
 
E

E. Robert Tisdale

Barry said:
You actually search on C++ to find stuff about C?

I knew that I could find stuff about serialization for C++
but I wasn't sure about C. I just searched for

+"serialization" +"C programming language"

and found lots of stuff but it appears to be more related
to Java, Python and C++.

If you can find a good serialization library in C,
please pass on the pointer.
 
D

Dan Pop

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.)

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.

Dan
 
F

Flash Gordon

On 7 Apr 2004 16:08:03 GMT
[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.

There is one other option. You convert the data in to your own
standardised binary format and use that. Of course, depending on the
data types you need to output and whether you are concerned about
implementations with bytes larger than 8 bits this can be non-trivial.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top