zehra...@[EMAIL PROTECTED]
wrote:
> 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));
This malloc allocates space for a char pointer. When you allocate
memory
for the structure, you will already have made space for the pointer.
You
are missing a place to store the string. What I think you
want is to
pCustData->szFirstName = (char *)malloc(sizeof(char)*the number of
chars you need)
It has been a while since I looked at this, but you will need to loop
fgetc looking for
your "end of line" or guess a large number with a buffer and use
fgets, and possibly some other
string functions to get the length (number of bytes that you wish to
allocate.
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|