Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

copying a struct to a binary file

0 views
Skip to first unread message

Norberto Grassi

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to
Hello,
I need to copy a struct to a binary file.

I think I need to use memcpy but I'm not sure:
can I do something like

memcpy(file_ptr, struct_ptr, sizeof(struct) );

?

Any help would be greatly appreciated.
Thank you,
N.G.

Paul D. Boyle

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to
Norberto Grassi (mailin...@iol.it) wrote:
: I need to copy a struct to a binary file.

: I think I need to use memcpy but I'm not sure:
: can I do something like

: memcpy(file_ptr, struct_ptr, sizeof(struct) );

No. You will need to use fwrite(). Consult a C programming book or your
compiler documentation for more detailed information on using fwrite().

Paul

--
Paul D. Boyle | bo...@laue.chem.ncsu.edu
Director, X-ray Structural Facility | phone: (919) 515-7362
Department of Chemistry - Box 8204 | FAX: (919) 515-5079
North Carolina State University |
Raleigh, NC, 27695-8204
http://laue.chem.ncsu.edu/web/xray.welcome.html

Paul Lutus

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to
In most cases, the best answer is, "Don't."

If the structure contains pointers, the file data will not behave if
reloaded into a similar data-reading program.

If you change compiler versions, existing data may not be readable.
If you change computer platforms, existing data may not be readable.

You are better off writing and reading the data in text form.

In any case, memcpy() is not the correct function.

Please think over your reasons for writing binary data to a file. And post
the relevant part of your code.

--

Paul Lutus
www.arachnoid.com

Norberto Grassi wrote in message <37712E6C...@iol.it>...
>Hello,


>I need to copy a struct to a binary file.
>
>I think I need to use memcpy but I'm not sure:
>can I do something like
>
>memcpy(file_ptr, struct_ptr, sizeof(struct) );
>

aravan

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to

I understand the reasoning behind not writing structs with pointers to a
binary file. But i fail to understand why one should not write (pointerless)
structures to a binary file. I used fwrite(&student, sizeof (student), 1,
fileptr); to write structs to files, but you are saying that this is not a
good idea? Does this have something to do with sizeof ? I guess i do not
understand why it would be invalid to do such a thing.

What types of data are "valid" to write to binary files?

aravan

Paul Lutus

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to
There have been several very long threads on this subject.

Imagine you are creating a database that must last years. You create your
first version and save the data to a file in binary form.

A. A new version of your compiler is released. It:

1. Has different sizes for some data types,
2. Packs structures using different rules,
3. Packs individual array structures differently, depending on where they
happen to be in memory.

Any of these will make the data unreadable.

B. You move to a new platform. Add to all of the above the big-endian,
little-endian issue, which is basically the order of bytes in a
multiple-byte data type.

C. In the fullness of time, the data must be moved to a new program, in a
new language, on a new platform. Forget it -- game over.

All of A, B, and C argue against saving in binary form. All of A, B, and C
can be accommodated if you will store your data in text form.

Add to this the fact that the text-format data file is human-readable, a
great asset in debugging existing code or writing new programs to read it.

There are portable ways to save data in binary form, but they are very
difficult to implement.
--

Paul Lutus
www.arachnoid.com

aravan wrote in message <37715323...@cis.ohio-state.edu>...

John Gordon

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to
aravan <merrych...@cis.ohio-state.edu> writes:

> I understand the reasoning behind not writing structs with pointers to a
> binary file. But i fail to understand why one should not write (pointerless)
> structures to a binary file. I used fwrite(&student, sizeof (student), 1,
> fileptr); to write structs to files, but you are saying that this is not a
> good idea? Does this have something to do with sizeof ? I guess i do not
> understand why it would be invalid to do such a thing.

> What types of data are "valid" to write to binary files?

the problem isn't that it's "invalid." fwrite()'ing a struct to a
binary file will work just fine.

but you're only guaranteed to be able to correctly _read_ that file on
the same computer with the same version of the compiler.

if you copy the file to another computer, or if you get a new version
of your compiler, you might not be able to correctly read the file.

---
John Gordon "No Silicon Heaven? Preposterous! Where would
gor...@jtan.com all the calculators go?" -- Kryten, Red Dwarf

Paul Lutus

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to
The proposal I have made has three parts:

1. You realize that binary storage has serious problems.
2. You decide to write and read in text mode.
3. You change your code to reflect this change -- that means no fwrite(), no
fread().

Do 1 and 2. Then do 3.

<< So now I have to go back and change my input/output methods to
fputs/fgets/fscanf/fprintf, right? >>

Yes. That is because saying that you are changing from binary to text
storage methods is not enough. You need to do it too.

--

Paul Lutus
www.arachnoid.com

aravan wrote in message <3771C7FA...@cis.ohio-state.edu>...
>Well, this presents me with problem. I used fwrite and fread to write and
read to
>a binary file. That's ok. But as a result of this thread, I changed the
fopen's
>from binary to text, everything still works OK. But wouldn't the fact that
I am
>using fread/fwrite to access text files be "bad"? So now I have to go back
and
>change my input/output methods to fputs/fgets/fscanf/fprintf, right? Or is
it OK
>to use fread/fwrite with text files?
>
>damn, I should have thought this thing out a little more!!
>
>aravan

Jarmo Tiittanen

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
Hi Norberto!

Norberto Grassi wrote:

> Hello,
> I need to copy a struct to a binary file.
>
> I think I need to use memcpy but I'm not sure:
> can I do something like
>
> memcpy(file_ptr, struct_ptr, sizeof(struct) );

I did the same thing something like this
fwrite(&structarray, sizeof (struct STRUCTS), 1, write_file);, but I
don't know is it a right way to do it.

Regards


--
Jarmo Tiittanen
Svanströminkuja 5-7 B22
00870 HELSINKI
http://www.dlc.fi/~jacxs
a980...@myy.helia.fi
j.o.ti...@dlc.fi

Jarmo Tiittanen

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
Hi aravan!

Please, read the original and Paul's message again.

Regards
--
Jarmo Tiittanen
Svanströminkuja 5-7 B22
00870 HELSINKI
http://www.dlc.fi/~jacxs

a980...@myy.helia.fiHi aravan!


j.o.ti...@dlc.fi

aravan

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to

Well, this presents me with problem. I used fwrite and fread to write and read

structures to
a binary file. That's not good apparently. So as a result of this thread, I


changed the fopen's
from binary to text, everything still works OK. But wouldn't the fact that I am
using fread/fwrite to access text files be "bad"? So now I have to go back and
change my input/output methods to fputs/fgets/fscanf/fprintf, right? Or is it OK

to use fread/fwrite to read/write structures to text files?

Stephan Wilms

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
aravan schrieb:

>
> Well, this presents me with problem. I used fwrite and fread to write and read
> structures to
> a binary file. That's not good apparently.

Correct ! It is not good, because it causes severe portability problems.

> So as a result of this thread, I changed the fopen's
> from binary to text, everything still works OK.

If so, you were very lucks. Changing the file type can lead to unexpected
problems.

> But wouldn't the fact that I am
> using fread/fwrite to access text files be "bad"?

Exactly, because there are important differences between the handling of
binary files and text files. The '\n' might be written differently between
text and binary files. There might be a byte representingan EOF, causing
your files to be unexpectedly short, if you accidentally write that byte.
All these things are highly implementation defined. This means that each
compiler is allowed (within the limits of ANSI-C) to find it's own way of
doing these things.

> So now I have to go back and
> change my input/output methods to fputs/fgets/fscanf/fprintf, right?

Yes, this is certainly what you should do. Use text file functions for
writing to text files.

> Or is it OK
> to use fread/fwrite to read/write structures to text files?

Theoretically yes, but practically it will present you with problems. I
strongly recommend against it.

> damn, I should have thought this thing out a little more!!

One last note: binary files can be used, if you know about their limits
and the potential portability problems. Often when storing huge amounts of


data you don't have a choice, because as John Gordon wrote:

> > you're only guaranteed to be able to correctly _read_ that file on
> > the same computer with the same version of the compiler.

That defines when it will work without problem. Using different compilers
or different compiler versions will require a compatibility analysis of
how the compiler stores data types and data structures.

Stephan
(initiator of the campaign against grumpiness in c.l.c)

aravan

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
since this is the case, what is the best/easiest way to write a struct to a text file
so that it can be read as a struct? using fprintf it seems that i would have to read
each struct member from the file seperately, which would seem to defeat the purpose
of using a struct! the same problem would seem to exist with fputs.

so how can i still use a struct, but read and write to text files AND keep the struct
intact?

Stephan Wilms wrote:

> aravan schrieb:
> >
> > Well, this presents me with problem. I used fwrite and fread to write and read
> > structures to
> > a binary file. That's not good apparently.
>
> Correct ! It is not good, because it causes severe portability problems.

> > So now I have to go back and

Paul Lutus

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
<< since this is the case, what is the best/easiest way to write a struct to
a text file
so that it can be read as a struct? >>

IMHO this question isn't phrased very well. You might instead ask "How do I
reliably write and read the data in my structures?"

The answer, is "as text" and "as text." You need to format the data as text
when you write it, so it can be reliably read again, using a parser that
reads the text and fills your structure with the appropriate representation
of the data.

--

Paul Lutus
www.arachnoid.com

aravan wrote in message <377252AE...@cis.ohio-state.edu>...

Lawrence Kirby

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
In article <3771C892...@cis.ohio-state.edu>
merrych...@cis.ohio-state.edu "aravan" writes:

>Well, this presents me with problem. I used fwrite and fread to write and read
>structures to

>a binary file. That's not good apparently. So as a result of this thread, I


>changed the fopen's
>from binary to text, everything still works OK.

Only by chance. Writing general binary data to a text file is an error and
isn't guaranteed to work.

Your fundamental problem is that by usign fwrite() you are exposing the
internal dataformats used by the compiler and these are inherenetly
compiler-specific. That will be the case whatever type of file you write
to. By writing binary data to a text file you don't solve any problems,
you just create more.

> But wouldn't the fact that I am

>using fread/fwrite to access text files be "bad"? So now I have to go back and
>change my input/output methods to fputs/fgets/fscanf/fprintf, right? Or is it


> OK
>to use fread/fwrite to read/write structures to text files?

You need to chage the code so that the format of the data you are writing
is portable. You can use functions like fprintf() to convert binary object
valuessuch as integers and floating point values to a text format.
fputs() etc. requite that the data is already in a text string format.

>damn, I should have thought this thing out a little more!!

There are ways of creating reasonably portable binary formats but these
still require specific conversion routines to and from a closely defined
format.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Chris Dollin

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
aravan wrote:
>
> I understand the reasoning behind not writing structs with pointers to
> a binary file. But i fail to understand why one should not write
> (pointerless) structures to a binary file. I used
> fwrite(&student, sizeof (student), 1,fileptr);
> to write structs to files, but you are saying that this is not a
> good idea?

Yes, in general it is not a good idea.

> Does this have something to do with sizeof ? I guess i do not
> understand why it would be invalid to do such a thing.

It's not *invalid*, it's just *unwise*.

On different implementations of C, data types like _int_ can have
different sizes and different bit/byte orderings. The compilers may
insert different amounts of padding between structure elements. It's
even possible that the types have different representations (although
there's not all that much room for manoeuver; for example, negabinary
isn't a legit representation for ints, boo hiss rotters).

So you're data isn't portable between implementations.

What's that? That doesn't matter?

You'll never upgrade your compiler? You'll never move to a different
architecture? You'll never want to share your data with a friend who
uses a different compiler or operating system or processor? Your
instructor doesn't run automatic test cases over your code -- after
compiling it on his Unix workstation?

It's much, much safer to define how your data is stored in the file
as a collection of characters, even if you have to write read & write
functions. It also gives you a convenient place to plant breakpoints :-)



> What types of data are "valid" to write to binary files?

Ascii characters are probably safe ...

--
9-bit-byte Hedgehog

Richard Heathfield

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
Chris Dollin <ke...@hpjavaux.cup.hp.com> wrote in article
<377267F9...@hpjavaux.cup.hp.com>...
> aravan wrote:

<snip>

>
> > What types of data are "valid" to write to binary files?
>
> Ascii characters are probably safe ...
>

... unless of course you then read the binary file into a program compiled
by C/370 under MVS/TSO, where the file has not undergone translation (such
translation would of course be the Wrong Thing for binary files anyway)
when being transferred between platforms.

--
Richard Heathfield

The bug stops here.


Lawrence Kirby

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
In article <377252AE...@cis.ohio-state.edu>
merrych...@cis.ohio-state.edu "aravan" writes:

>since this is the case, what is the best/easiest way to write a struct to a
> text file
>so that it can be read as a struct? using fprintf it seems that i would have
> to read
>each struct member from the file seperately,

Yes, to do this portably you must deal with each member separately.

>which would seem to defeat the
> purpose
>of using a struct! the same problem would seem to exist with fputs.

The purpose of using a struct is to group data together as a single unit
within the program. Having to break it apart for I/O doesn't defeat this
at all.

>so how can i still use a struct, but read and write to text files AND keep the
> struct
>intact?

You might, say, put the contents of the struct on a single line. That
keeps it all together. You must however perform the conversion for each
structure member individually. However it is generally pretty trivial
to write a function to do that (reading may be a bit trickier than writing).

Herbert Rosenau

unread,
Jun 27, 1999, 3:00:00 AM6/27/99
to
Norberto Grassi wrote:
>
> Hello,
> I need to copy a struct to a binary file.
>
> I think I need to use memcpy but I'm not sure:
> can I do something like
>
> memcpy(file_ptr, struct_ptr, sizeof(struct) );
>
Do you know the difference between memory and a file?

I'd read the whole thread!

If you would have large data you would use binary files to save space on
disk - a binary int would save 50% or more disk space.

- use never pointers in files, convert them to ordinals.
- write member by member of your structure with a defined size of bytes
if you must aware that sizeof(type) or alignment of struct members my
change in a new compiler.
- hold in mind: you my have to convert the file if you use another
compiler/OS/hardware. But the converting would be easier if you handle
all struct members separate.

For now you my write/read ech struct member directly by using
fwrite()/fread().

On other hand: if you have to change the underliiny OS and hardware you
my write a little translator to convert the binary fie for the new
hardware (byte alignment, size and structure of binary datum (int,
float) without changing the source of your program.

You should check the size the file has as binary and as text. The
textfile would up to 10 times larger! A binary long has only 4 bytes, as
text you must spend at least 13. A float comes with 4 or 10 bytes binary
- as test up to 32 - with lost on precision.

At least: to read/write and convert from/to binary is very slow. to
write binary as is will tune up your process significantly.

0 new messages