The following code example writes a structure variable of type EMPLOYEE to a file, and then, using fread, reads the structure back into another variable.
int main()
{
EMPLOYEE e1,e2;
FILE *fptr;
e1.emp_id=2240;
e1.emp_name="Ravi Shekhar";
e1.emp_salary=10000;
fptr=fopen("c:\\employee.emp","w+b");
if(fptr == NULL)
{
printf_s("\n\t cannot open file. . .");
return 1;
}
printf_s("%d records written successfully. . .",fwrite(&e1,sizeof(EMPLOYEE),1,fptr));
fseek(fptr,0,SEEK_SET);
fread(&e2,sizeof(EMPLOYEE),1,fptr);
printf_s("\nID = %d\nName = %s\nSalary = %10.2lf",e2.emp_id,e2.emp_name,e2.emp_salary);
fclose(fptr);
_getch();
return 0;
}
My question is where and how the name string indicated by e1.emp_name (type char *) is stored in a binary file.
Thank.
source
share