Re: Problem in writing structure to Binary file in C lang



On Apr 19, 2:11 am, zehra...@xxxxxxxxx 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));

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.............
--
comp.lang.c.moderated - moderation address: c...@xxxxxxxxxxxx -- 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.

---pCustdata = malloc(sizeof(CUST_DATA));
By above statement .. you are allocating the memory for structure..
--pCustData->szFirstName = malloc(sizeof(pCustData->szFirstName))
By above statement you are allocating only "sizeof(pCustData-
szFirstName))" which may be max of 4 .. Try with allocating more
memory to this structure ....
like .. pCustData->szFirstName = malloc(sizeof(char) * 100);
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- 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.
.



Relevant Pages

  • Re: Sharing with Newsgroup
    ... with MS to forbid attachments, delete them at source, rather than ... > Many hosts that carry newsgroups prohibit binary posts in text-only ... > Some services impose limits on the rate at which a customer can ... > large binary file can result in that limit being exceeded. ...
    (microsoft.public.excel.programming)
  • Re: Assigning Variables at runtime?
    ... > I'm a newbie to Delphi, and newsgroups so please be patient. ... > I have a binary file containing blocks of 194 bytes of data. ...
    (comp.lang.pascal.delphi.misc)
  • Problem in writing structure to Binary file in C lang
    ... reading those data from binary file and display it in C language. ... typedef struct cust_data CUST_DATA; ... inplace of szFirstName it writes pointer to string. ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)
  • Re: Newsgroups - was Interested in joining the Masons...
    ... Newsgroup server at the end of December as "Newsgroups are an obsolete ... One needed to peer with other servers, which isn't free, there was a need for massive storage as well as bandwidth to handle an incoming and outgoing stream. ... And with the 35% overhead needed to transmit a binary file on a text-only system, you're talking about files as big as 10 gigs. ... Comcast did that a couple years ago and contracts with Giganews, providing Comcast users ...
    (soc.org.freemasonry)
  • Assigning Variables at runtime?
    ... I'm a newbie to Delphi, and newsgroups so please be patient. ... I have a binary file containing blocks of 194 bytes of data. ...
    (comp.lang.pascal.delphi.misc)