469,613 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Writing a structure

Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??
thanx
glen
Nov 14 '05 #1
29 3203

"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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
Nov 14 '05 #2

"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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
Nov 14 '05 #3
Mike Wahler wrote:
"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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.


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.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rogério Brito - rb****@ime.usp.br - http://www.ime.usp.br/~rbrito
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Nov 14 '05 #4
Mike Wahler wrote:
"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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.


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.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rogério Brito - rb****@ime.usp.br - http://www.ime.usp.br/~rbrito
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Nov 14 '05 #5
In <eb**************************@posting.google.com > gl********@yahoo.co.in (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.

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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
In <eb**************************@posting.google.com > gl********@yahoo.co.in (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.

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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
Rogério Brito wrote:
Mike Wahler wrote:
Glen wrote:
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.


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.

Nov 14 '05 #8
Rogério Brito wrote:
Mike Wahler wrote:
Glen wrote:
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.


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.

Nov 14 '05 #9
> 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...
Nov 14 '05 #10
> 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...
Nov 14 '05 #11

"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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...

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
Nov 14 '05 #12

"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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...

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
Nov 14 '05 #13
Glen wrote:
Another way would be to write
a textual version, one member at a time.


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.

Nov 14 '05 #14
Glen wrote:
Another way would be to write
a textual version, one member at a time.


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.

Nov 14 '05 #15
E. Robert Tisdale wrote:
Rogério Brito wrote:
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.


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.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rogério Brito - rb****@ime.usp.br - http://www.ime.usp.br/~rbrito
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Nov 14 '05 #16
E. Robert Tisdale wrote:
Rogério Brito wrote:
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.


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.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rogério Brito - rb****@ime.usp.br - http://www.ime.usp.br/~rbrito
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Nov 14 '05 #17
In <c4*************@ID-218953.news.uni-berlin.de> =?ISO-8859-1?Q?Rog=E9rio_Brito?= <rb****@ime.usp.br> writes:
Mike Wahler wrote:
"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #18
In <c4*************@ID-218953.news.uni-berlin.de> =?ISO-8859-1?Q?Rog=E9rio_Brito?= <rb****@ime.usp.br> writes:
Mike Wahler wrote:
"Glen" <gl********@yahoo.co.in> wrote in message
news:eb**************************@posting.google.c om...
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #19
Da*****@cern.ch (Dan Pop) writes:
In <eb**************************@posting.google.com >
gl********@yahoo.co.in (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.)

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #20
Da*****@cern.ch (Dan Pop) writes:
In <eb**************************@posting.google.com >
gl********@yahoo.co.in (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.)

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #21
kal
> Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??


Convert the "data" in the structure to an XML document.
Nov 14 '05 #22
kal
> Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??


Convert the "data" in the structure to an XML document.
Nov 14 '05 #23
On Tue, 06 Apr 2004 09:15:51 -0700, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
I used Google

http://www.google.com/

to search for

+"C++" +"serialization"

and I found lots of stuff.


You actually search on C++ to find stuff about C?
<<Remove the del for email>>
Nov 14 '05 #24
On Tue, 06 Apr 2004 09:15:51 -0700, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
I used Google

http://www.google.com/

to search for

+"C++" +"serialization"

and I found lots of stuff.


You actually search on C++ to find stuff about C?
<<Remove the del for email>>
Nov 14 '05 #25
Barry Schwarz wrote:
E. Robert Tisdale wrote:
I used Google

http://www.google.com/

to search for

+"C++" +"serialization"

and I found lots of stuff.


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.

Nov 14 '05 #26
Barry Schwarz wrote:
E. Robert Tisdale wrote:
I used Google

http://www.google.com/

to search for

+"C++" +"serialization"

and I found lots of stuff.


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.

Nov 14 '05 #27
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Da*****@cern.ch (Dan Pop) writes:
In <eb**************************@posting.google.com >
gl********@yahoo.co.in (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.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #28
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Da*****@cern.ch (Dan Pop) writes:
In <eb**************************@posting.google.com >
gl********@yahoo.co.in (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.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #29
On 7 Apr 2004 16:08:03 GMT
Da*****@cern.ch (Dan Pop) wrote:
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org>
writes:
Da*****@cern.ch (Dan Pop) writes:
In <eb**************************@posting.google.com >
gl********@yahoo.co.in (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.
--
Flash Gordon
Somtimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #30

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by DanielEKFA | last post: by
15 posts views Thread by Glen | last post: by
9 posts views Thread by curious_one | last post: by
8 posts views Thread by mojozoox | last post: by
2 posts views Thread by DBC User | last post: by
5 posts views Thread by Phil Kelly | last post: by
6 posts views Thread by arne.muller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.