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.da t","ab+");
return fp;
pCustdata = malloc(sizeof(C UST_DATA));
pCustData->szFirstName = malloc(sizeof(p CustData-
printf("Enter CAFID : ");
scanf("%d",&pCu stData->nCAFID);
printf("Enter First Name : ");
scanf("%s",pCus tData->szFirstName) ;
fwrite(pCustDat a,sizeof(CUST_D ATA),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........ .....
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.da t","ab+");
return fp;
pCustdata = malloc(sizeof(C UST_DATA));
pCustData->szFirstName = malloc(sizeof(p CustData-
>szFirstName) );
scanf("%d",&pCu stData->nCAFID);
printf("Enter First Name : ");
scanf("%s",pCus tData->szFirstName) ;
fwrite(pCustDat a,sizeof(CUST_D ATA),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........ .....
Comment