Problem in writing structure to Binary file in C lang

Z

zehra.mb

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.
Here is my part of my code.

struct cust_data
{
int nCAFID;
char *szFirstName;
};

typedef struct cust_data CUST_DATA;
CUST_DATA *pCustdata = NULL;
int AddRecord()
{
FILE *fp;
fp = fopen("Input.dat","ab+");
return fp;
pCustdata = malloc(sizeof(CUST_DATA));

pCustData->szFirstName = malloc(sizeof(pCustData-
szFirstName));

printf("Enter CAFID : ");
scanf("%d",&pCustData->nCAFID);

printf("Enter First Name : ");
scanf("%s",pCustData->szFirstName);

fwrite(pCustData,sizeof(CUST_DATA),1,fp);

fclose(fp);

free(pCustData->szFirstName);
free(pCustData)
}

now here when i write a structure to file, it writes nCAFID and
inplace of szFirstName it writes pointer to string.
But i need to write string..
if i take szFirstName as char array then it is wroking fine.....but i
want to use heap memory instead of stack..
can anyone help me..................??
thanks in advance.............
 
J

Jens Thoms Toerring

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.

That's usually not a good idea since then you probably won't be
able to read in that file on a machine with a different archi-
tecture and maybe even when you compile the program with a dif-
ferent compiler (or compiler version or options) on the same
machine! Moreover, you run into problems if the struture contains
pointers, as you already found out.
Here is my part of my code.
struct cust_data
{
int nCAFID;
char *szFirstName;
};
typedef struct cust_data CUST_DATA;
CUST_DATA *pCustdata = NULL;
int AddRecord()
{
FILE *fp;
fp = fopen("Input.dat","ab+");
return fp;
pCustdata = malloc(sizeof(CUST_DATA));
pCustData->szFirstName = malloc(sizeof(pCustData->szFirstName));

Here you already have a problem. You allocate just enough memory
to store a pointer, but rather likely not enough memory to store
a string (unless it's a very short one).
printf("Enter CAFID : ");
scanf("%d",&pCustData->nCAFID);
printf("Enter First Name : ");
scanf("%s",pCustData->szFirstName);

And if the string the user entered is longer that the size of a
pointer to a string then you write over memory yiu don't own.
If you're lucky you will get an immediate segmentation fault
bur if you're unlucky the program will look as if it's working
and crash in some seemingly unrelated place.

Just some hint: using scanf() this way is inherently unsafe.
This way the user can always enter a string that's longer
than you allocated. If you use scanf() then you will have
to specify a maximum length of the string to be read in to
avoid that (just put the number between the '%' and the 's'
or put a '*' in between and make the maximum number of
characters the next argument of scanf()).
fwrite(pCustData,sizeof(CUST_DATA),1,fp);


now here when i write a structure to file, it writes nCAFID and
inplace of szFirstName it writes pointer to string.
But i need to write string..
if i take szFirstName as char array then it is wroking fine.....but i
want to use heap memory instead of stack..

It's not something related to "stack" or "heap" memory. The
problem is that you write just a pointer to the file. And that
pointer value only makes sense while the program that writes
out the structure is still running and hasn't deallocated the
memory the pointer points to. Once the memory has been de-
allocated or when you try to read in the structure with a
new instance of the program (or a different one) the pointer
points to some memory location where there are just random
data (or may even point to memory you're not allowed to
access).

Trying to store a pointer to something instead of the real
thing is like putting a foto of your birthday cake into the
fridge and throwing the cake away. While that may safe some
room in the fridge don't be surprised if the foto doen't
taste as well as the real cake;-)

Actually, if it would be possible to just store pointers
and still get back what they were pointing to you wouldn't
have to store the structure but you could instead just store
the pointer to the structure itself.

So the only reasonable way to write out a structure to a file
is to "serialize" its data. Instead of simply writing out the
structure to a binary file write it's contents in e.g. ASCII,
i.e. first a line with the 'nCAFID' number, then another one
with the string. Only that will allow you to get back the data
in all possible circumstances.

Regards, Jens
 
Z

zehra.mb

Thaks a lot for the reply..
(e-mail address removed) said:






What if it fails?


Oops - fp is not an int. (I suspect this is simply accidental or forgotten
code.)

sorry thats my mistake..my code is like
if(!fp)
return 0;
What if it fails?




This will allocate enough bytes to store a pointer, which isn't what you
want. Decide how many bytes to allocate for storing a first name and
allocate that many, or simply make szFirstName an array of char and drop
the szFirstName malloc completely.



You run the risk of having your user enter (accidentally or maliciously)
more characters than you can store, resulting in a buffer overrun.




This won't work if you dynamically allocate szFirstName - you'd have to
write it separately and make a note of the size so that you can read it
back in. Making szFirstName an ordinary array will fix this.

ya thats true.
As i mentioned it works fine if i take simple array of characters..
I found what is the problem.Here i am writing pointer to strig in file
and after using the structure i am deaalocation memory for it.
so next time when it reads from a file and try to get value at that
pointer,it wont get the actual data.
so may be i need to serialize the data.

Regards,
Zehra
 
Z

zehra.mb

Thaks a lot for the reply..






sorry thats my mistake..my code is like
if(!fp)
return 0;







ya thats true.
As i mentioned it works fine if i take simple array of characters..
I found what is the problem.Here i am writing pointer to strig in file
and after using the structure i am deaalocation memory for it.
so next time when it reads from a file and try to get value at that
pointer,it wont get the actualdata.
so may be i need toserializethedata.

Regards,
Zehra

Hi Jens,
Thanks for the reply.
you are right.
Can you please tell me how would i serialize data in C(can you please
provide example code)?
Also for scanf now i have written

printf("Enter First Name : ");
scanf("%50s",pCustData->pszFirstName);
printf("Enter Last Name : ");
scanf("%50s",pCustData->pszLastName);

i want the name should not acceed 50 char,but here in this case if i
give 52 chars for fisrt name as input it will take 50 for first name
and rest 2 for last name.
it doesnt allow me to enter last name.
So what i sould do to get only 50 chars and discard rest 2 chars?

Thanks & Regards,
Zehra
 
S

santosh

Can you please tell me how would i serialize data in C(can you please
provide example code)?

It's usually better to use existing code than to reinvent the wheel.

<http://tpl.sourceforge.net/>
Also for scanf now i have written

printf("Enter First Name : ");

To ensure that the above prompt is written to stdout immediately you
should call fflush(stdout) after the printf call.
scanf("%50s",pCustData->pszFirstName);
printf("Enter Last Name : ");

Similarly here too.
scanf("%50s",pCustData->pszLastName);

i want the name should not acceed 50 char,but here in this case if i
give 52 chars for fisrt name as input it will take 50 for first name
and rest 2 for last name.
it doesnt allow me to enter last name.
So what i sould do to get only 50 chars and discard rest 2 chars?

You can use a small function that reads and discards unwanted characters
in the input stream up to the newline character or EOF. Numerous source
examples have been posted here. Do a search of Google Groups' archives
of c.l.c with a search term like "discard input".

However a better approach might be to avoid scanf altogether (since it's
difficult to use in a water-tight manner), and read in lines one by one
with fgets and parse them with sscanf and other functions. A fgets
replacement for the above calls would be like:

if (fgets(input_buffer, input_buffer_length, stdin) == NULL) {
/* A return of NULL means there was a problem. You can deal with
that here.
*/
}

The problem of overlong input still remains. You still need to discard
any input remaining in the stream and check that fgets has read a
complete line. You could treat incomplete reads as either a normal
situation or as an error. That would be program specific.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top