Hi all,
I've been wondering when I write a structure like:
struct {
int a;
unsigned int b;
float c;
} mystruct;
And then I'm using this as a record for a binary file. The problem is
that the size of the types is different on different
platforms(win/lin/osx) so if a file was copied on another platform and
attempted to be read then the first say 16 bytes could be regarded as
the integer a but it could have been created on system where integer
was 32 bytes. Is there a portable solution to this? Moreover, I've been
looking for some resource on designing your own binary format and I
couldn't find anything apart from short tutorials how to read binary
files. Are there any good resources?
Thanks a lot 9 2480
On 30 Dec 2005 16:05:03 -0800, in comp.lang.c , "gamehack"
<ga******@gmail.com> wrote: Hi all,
I've been wondering when I write a structure like:
struct { int a; unsigned int b; float c; } mystruct;
And then I'm using this as a record for a binary file. The problem is that the size of the types is different on different platforms(win/lin/osx) so if a file was copied on another platform and attempted to be read then the first say 16 bytes could be regarded as the integer a but it could have been created on system where integer was 32 bytes. Is there a portable solution to this?
The simplest is to store the data as text, not binary data. Other
methods might involve using fixed-width data types (if your platforms
support them), or writing custom load/save functions for each platform
which still store in binary but do it element by element and take into
account the differing sizes of types on each platform.
Mark McIntyre
--
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
gamehack wrote: I've been wondering when I write a structure like:
struct { int a; unsigned int b; float c; } mystruct;
And then I'm using this as a record for a binary file. The problem is that the size of the types is different on different platforms(win/lin/osx) so if a file was copied on another platform and attempted to be read then the first say 16 bytes could be regarded as the integer a but it could have been created on system where integer was 32 bytes.
Good. You recognize the existence of a problem. The answer is
"Don't do that". Binary representations are, in general, not
portable. You can convert things into a sequence of bytes and
write/read those to a file, but that means you also have to write
the conversion mechanisms. Now such things as byte sex can bite you.
Far and away the most portable transportation mechanism is pure
text. You already have conversion routines in the standard
library, and all you need to do is use them. Anybody and their dog
can read the files.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
"gamehack" <ga******@gmail.com> wrote I've been wondering when I write a structure like:
struct { int a; unsigned int b; float c; } mystruct;
And then I'm using this as a record for a binary file. The problem is that the size of the types is different on different platforms(win/lin/osx) so if a file was copied on another platform and attempted to be read then the first say 16 bytes could be regarded as the integer a but it could have been created on system where integer was 32 bytes. Is there a portable solution to this? Moreover, I've been looking for some resource on designing your own binary format and I couldn't find anything apart from short tutorials how to read binary files. Are there any good resources?
Integers are easy. Just use the AND and OR operators, together with the
bitshifts ( >> <<) to break up an integer into 8-bit chunks, and store it,
big-endian, in a file.
It is necessary to use the big-endian format because otherwise those
little-endians might take over the world, and force us all to store our
bytes at the little end, and we don't wnat that happening.
The float is a bit more tricky. Floating point number have their own
internal format. The good news is that virtually all are 32-bit IEEE format
(sign, exponent, mantissa). You can probably get away with a binary dump,
making sure of the endianness. However to be really portable, you do need to
break the number up into its constitutents, and then rebuild it, using the
ldexp() and frexp() functions.
Chuck F. wrote: gamehack wrote:
I've been wondering when I write a structure like:
struct { int a; unsigned int b; float c; } mystruct;
And then I'm using this as a record for a binary file. The problem is that the size of the types is different on different platforms(win/lin/osx) so if a file was copied on another platform and attempted to be read then the first say 16 bytes could be regarded as the integer a but it could have been created on system where integer was 32 bytes.
Good. You recognize the existence of a problem. The answer is "Don't do that". Binary representations are, in general, not portable. You can convert things into a sequence of bytes and write/read those to a file, but that means you also have to write the conversion mechanisms. Now such things as byte sex can bite you.
Far and away the most portable transportation mechanism is pure text. You already have conversion routines in the standard library, and all you need to do is use them. Anybody and their dog can read the files.
(Please excuse the vacuous reply that I fat-fingered
a moment ago.)
Chuck F. wrote: gamehack wrote:
I've been wondering when I write a structure like:
struct { int a; unsigned int b; float c; } mystruct;
And then I'm using this as a record for a binary file. The problem is that the size of the types is different on different platforms(win/lin/osx) so if a file was copied on another platform and attempted to be read then the first say 16 bytes could be regarded as the integer a but it could have been created on system where integer was 32 bytes.
Good. You recognize the existence of a problem. The answer is "Don't do that". Binary representations are, in general, not portable. You can convert things into a sequence of bytes and write/read those to a file, but that means you also have to write the conversion mechanisms. Now such things as byte sex can bite you.
"Don't do that" needs a little qualification, I think.
If "that" means "just read and write the struct in whatever
form the compiler happens to choose," the advice is sound.
But the claim that binary representations are not portable
(I'm not sure what "in general" means here) doesn't hold up.
Who has not transported a ZIP or GIF or JPEG file between
dissimilar systems? At a lower level, who has not exchanged
IP packets with other systems? Portability is a matter of
agreed-upon standards, not of the underlying representations
chosen.
Far and away the most portable transportation mechanism is pure text. You already have conversion routines in the standard library, and all you need to do is use them. Anybody and their dog can read the files.
Text has a few pitfalls of its own. Even without appealing
to the multitude of character encoding schemes, some difficulties
are apparent. For example, it is no simple matter to devise a
portable text representation for arbitrary `double' values. A
value encoded as text, sent to another machine and decoded, then
re-encoded and sent back again may not decode to the same value
that was originally transmitted. It requires as much care to
make this work for text as for binary representations. (And I've
got the war stories from a PPOE to prove it, too ...)
--
Eric Sosman es*****@acm-dot-org.invalid
Eric Sosman <es*****@acm-dot-org.invalid> writes:
[...] Text has a few pitfalls of its own. Even without appealing to the multitude of character encoding schemes, some difficulties are apparent. For example, it is no simple matter to devise a portable text representation for arbitrary `double' values. A value encoded as text, sent to another machine and decoded, then re-encoded and sent back again may not decode to the same value that was originally transmitted. It requires as much care to make this work for text as for binary representations. (And I've got the war stories from a PPOE to prove it, too ...)
A hexadecimal floating-point representation (supported in C99,
implementable in C90) should avoid at least some of the problems.
With enough digits, you can have an exact textual representation of a
floating-point value.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Thank you. That's why I wondered how to design a format, like .zip .jpg
etc :) Do you basically say that each 33 bytes would be one pixel, and
the value of red would be the first 11 bytes, green next 11 bytes, and
then last 11 bytes are going to be blue. And probably some fixed-size
headers at the end file(or probably using some sequence of bytes to
mark end of fields in the header). The problem is that I haven't seen
_any_ good resources about designing file formats. Any pointers?
Regards,
gamehack
gamehack wrote: Thank you. That's why I wondered how to design a format, like .zip .jpg etc :) Do you basically say that each 33 bytes would be one pixel, and the value of red would be the first 11 bytes, green next 11 bytes, and then last 11 bytes are going to be blue. And probably some fixed-size headers at the end file(or probably using some sequence of bytes to mark end of fields in the header). The problem is that I haven't seen _any_ good resources about designing file formats. Any pointers?
<OT>
Visit http://www.wotsit.org/ to find descriptions of
many file formats. Some are binary, some are textual. Some
are designed for portability, some are not. In any event, a
review of what's already been done should give you some ideas.
Perhaps you'll even find an existing format that meets your
needs; if so, adopting it might make available whole suites of
helpful tools for dealing with it.
</OT>
--
Eric Sosman es*****@acm-dot-org.invalid This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andrew |
last post by:
This is a homework question that has gone passed it's due date,
however I still want to figure it out. This program initializes three
structures, writes them to a binary file, reads them back out...
|
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: Jens Mittag |
last post by:
Hi!
In my code, I have an array of a structure, which I want to save to a
binary file. When the array is just created, everything works fine, but
when I change contents of the array, saving...
|
by: BobTheHacker |
last post by:
I guess I am simply blind this morning. I have a binary file I want to read into a predefined structure of ints/strings/shorts and arrays. How do I go about this. In C++ I would simply define a...
|
by: imme929 |
last post by:
How do I do it? Nothing in the books is helpful.
I need to save a structure with different data types.
| |
by: John Dann |
last post by:
Trying to declare a structure that will contain a couple of fixed-size
arrays. I'm trying eg:
Structure IndexRecord
Dim testarray(16) as Byte
etc
End Structure
But vb.net is refusing to let...
|
by: tony.fountaine |
last post by:
I am working on a project to read a Bosch Measurement Data File (MDF).
The file contains a number of blocks that can be read from the file
using a baisc structure.
For example the ID BLOCK is as...
|
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: bernd |
last post by:
Hi folks,
a simple question for the experts, I guess.
Obviously I am doing something wrong when trying to access an element
of an array declared within a structure:
#include <stdio.h>...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |