ze******@gmail.com wrote:
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);
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..
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
--
\ Jens Thoms Toerring ___
jt@toerring.de
\__________________________
http://toerring.de