Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??
thanx
glen 29 3521
"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
"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
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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
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
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.
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.
> 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...
> 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...
"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
"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
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.
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.
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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
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 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" 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"
> 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.
> 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.
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>>
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>>
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.
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.
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DanielEKFA |
last post by:
Hey there :)
I was once told that the STL classes had member functions to write their
data to disk and to restore that data. Searching google (and why are there
no "stl" or "map" manpages?), it...
|
by: Glen |
last post by:
Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??
thanx
glen
|
by: curious_one |
last post by:
All,
I have a struct
struct {
char a;
char b;
}some_struct;
I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is...
|
by: mojozoox |
last post by:
Could you tell me scenarios in which core's dumped when writing to a
pipe.
write (pipefd,&Struct,structsize);
on windows too this give a memory "read" violation. Could anyone tell
me why.
...
|
by: DBC User |
last post by:
Hi Sharpies,
I have a C program I am converting it into C#. Everything is fine
except this process creates a 6K byte binary file. This file initially
filled with 6K null and then start...
|
by: Phil Kelly |
last post by:
Hi
I need to write the contents of a structure to a binary file - there is one
string and 2 integers, but I can't seem to figure out how to write the data
correctly.
If I am simply writing...
|
by: tim |
last post by:
Someone using Python Midi Package from
http://www.mxm.dk/products/public/ lately?
I want to do the following :
write some note events in a midi file
then after doing that, put some controllers...
|
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: zehra.mb |
last post by:
Hi,
I had written application for storing employee data in binary file and
reading those data from binary file and display it in C language.
But I face some issue with writing data to binary file....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |