473,471 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it possible to write a struct to binary file?

Hi there,
I'm thinking of how to write a struct to a binary file, could anyone
help me with this?

for example

typedef struct record{

char *name
int contact
}rec;

file1 = fopen(..,"wb");
...
fwrite(rec, sizeof(rec),1,file1);

I've tried the above but it doesn't seem to work properly. Could anyone
help me.
thanks

Apr 4 '06 #1
4 6033
bergko opined:
Hi there,
I'm thinking of how to write a struct to a binary file, could anyone
help me with this?

for example

typedef struct record{

char *name
int contact
}rec;

file1 = fopen(..,"wb");
..
fwrite(rec, sizeof(rec),1,file1);

I've tried the above but it doesn't seem to work properly. Could


Of course it doesn't -- for various reasons. Most important one being
that your `rec` is type not a variable. If you used `sizeof` as a
function to shut up your compiler, you shouldn't have.

Below is some code that demonstrates what you may want, but beware, it
has no error checking whatsoever, and makes some assumptions that may
not hold for you (e.g. that "testfile" is a valid file name). It does
compile cleanly, and runs as expected on my Linux box.

/* CAVEAT EMPTOR */

#include <stdio.h>

typedef struct record{
char *name;
int contact;
} rec ;
int main(void)
{

FILE *file1;
rec a_rec = {NULL, 42};
rec b_rec = {NULL, 24};

printf("%d\n",a_rec.contact);
printf("%d\n",b_rec.contact);

file1 = fopen("testfile","wb");
fwrite(&a_rec, sizeof a_rec, 1, file1);
fclose(file1);

fopen("testfile","rb");
fread(&b_rec, sizeof b_rec, 1, file1);
fclose(file1);

printf("%d\n",a_rec.contact);
printf("%d\n",b_rec.contact);

return 0;
}
--
It's amazing how much "mature wisdom" resembles being too tired.

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 4 '06 #2
bergko wrote:

I'm thinking of how to write a struct to a binary file, could
anyone help me with this?

for example

typedef struct record{
char *name
int contact
}rec;

file1 = fopen(..,"wb");
..
fwrite(rec, sizeof(rec),1,file1);

I've tried the above but it doesn't seem to work properly. Could
anyone help me.


It is working fine. However what it writes is meaningless when
read back, since the pointer to name almost certainly will be
invalid.

--
"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/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 4 '06 #3
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
bergko opined:
typedef struct record{

char *name
int contact
}rec;

file1 = fopen(..,"wb");
..
fwrite(rec, sizeof(rec),1,file1);

I've tried the above but it doesn't seem to work properly. Could


Of course it doesn't -- for various reasons. Most important one being
that your `rec` is type not a variable. If you used `sizeof` as a
function to shut up your compiler, you shouldn't have.


More to the point, _it_ shouldn't have.
Below is some code that demonstrates what you may want, but beware, it
has no error checking whatsoever, and makes some assumptions that may
not hold for you (e.g. that "testfile" is a valid file name). It does
compile cleanly, and runs as expected on my Linux box. typedef struct record{
char *name;
int contact;
} rec ; rec a_rec = {NULL, 42};
rec b_rec = {NULL, 24}; fwrite(&a_rec, sizeof a_rec, 1, file1); fread(&b_rec, sizeof b_rec, 1, file1);


Another, more important, catch is that this you can only expect to read
back a valid pointer value if it was null to begin with, or if you are
still running the same execution of the program and haven't deallocated
the memory that the pointer pointed at.
For example, writing a pointer to an automatically allocated array and
then reading it back in another call of the same function may easily put
that array in another place in memory, making the pointer invalid.
Writing a pointer in one run of the program, and reading it back in
another, is even more likely to do the same.

Richard
Apr 4 '06 #4

Richard Bos wrote:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
bergko opined:

typedef struct record{

char *name
int contact
}rec;

file1 = fopen(..,"wb");
..
fwrite(rec, sizeof(rec),1,file1);

I've tried the above but it doesn't seem to work properly. Could


Of course it doesn't -- for various reasons. Most important one being
that your `rec` is type not a variable. If you used `sizeof` as a
function to shut up your compiler, you shouldn't have.


More to the point, _it_ shouldn't have.


Yes, for the first mention of `rec`. Seeing OP had typed the code
without care to make it barely correct, I disregarded that bit.
Below is some code that demonstrates what you may want, but beware, it
has no error checking whatsoever, and makes some assumptions that may
not hold for you (e.g. that "testfile" is a valid file name). It does
compile cleanly, and runs as expected on my Linux box.

typedef struct record{
char *name;
int contact;
} rec ;

rec a_rec = {NULL, 42};
rec b_rec = {NULL, 24};

fwrite(&a_rec, sizeof a_rec, 1, file1);

fread(&b_rec, sizeof b_rec, 1, file1);


Another, more important, catch is that this you can only expect to read
back a valid pointer value if it was null to begin with, or if you are
still running the same execution of the program and haven't deallocated
the memory that the pointer pointed at.
For example, writing a pointer to an automatically allocated array and
then reading it back in another call of the same function may easily put
that array in another place in memory, making the pointer invalid.
Writing a pointer in one run of the program, and reading it back in
another, is even more likely to do the same.


You are, of course, right. I managed to overlook (as in "saw but did
not see") the fact that one member is actually a pointer. It also
wasn't clear (to me) what exactly was OP's problem: getting it to
compile and run, or perform as expected (whatever it was that was
expected).

Apr 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
20
by: cylin | last post by:
Dear all, I open a binary file and want to write 0x00040700 to this file. how can I set write buffer? --------------------------------------------------- typedef unsigned char UCHAR; int...
26
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier...
0
by: Bahaa Hany | last post by:
that the code i wrote FileStream w = new FileStream("d:\\HufmanCompression.txt",FileMode.Create); BinaryWriter writer = new BinaryWriter(w); for (i = 0; i <...
13
by: zach | last post by:
Can someone help me out, I can't figure out what I'm doing wrong to write to a file in binary mode. What's wrong with my code? <?php $fileName = "something.dat"; $string = "This is a...
2
by: Vikashag | last post by:
Hi When I am trying to write in a binary file its giving me error. Now What I have to do? and in this Base64Decode2 is converted Binary data. Its giving the errror Arguments are of the...
5
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....
0
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...
0
jinu1996
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...
1
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...
0
tracyyun
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...
1
isladogs
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.