How do i wrtie more than one structure to a same file and then read it?
Here is a sample program i wrote.Tell me whats wrong with it
#include <stdio.h>
#define WRD 2
typedef unsigned char byte;
typedef struct
{
byte hr;
byte trans_no[WRD];
} ABC;
typedef struct
{
byte trans_no[WRD];
}XYZ;
main()
{
ABC ab;
ab.hr=9;
ab.trans_no[WRD]=20;
FILE *fp;
fp = fopen("testfile","w"
fwrite(&ab,sizeof(ABC),1,fp);
// fclose(fp);
XYZ xy;
xy.trans_no[WRD]=100;
// fp = fopen("testfile","a"
fwrite(&xy,sizeof(XYZ),1,fp);
fclose(fp);
FILE *fp1;
fp1 = fopen("testfile","r"
fread(&ab,sizeof(ABC),1,fp1);
printf("Hour is %u \n",ab.hr);
printf("Number is %d \n",ab.trans_no[WRD]);
// fclose(fp1);
// fp1 = fopen("testfile","r"
fread(&xy,sizeof(XYZ),1,fp1);
printf("Number from XYZ is %d \n",xy.trans_no[WRD]);
fclose(fp1);
}
Here is a sample program i wrote.Tell me whats wrong with it
#include <stdio.h>
#define WRD 2
typedef unsigned char byte;
typedef struct
{
byte hr;
byte trans_no[WRD];
} ABC;
typedef struct
{
byte trans_no[WRD];
}XYZ;
main()
{
ABC ab;
ab.hr=9;
ab.trans_no[WRD]=20;
FILE *fp;
fp = fopen("testfile","w"
fwrite(&ab,sizeof(ABC),1,fp);
// fclose(fp);
XYZ xy;
xy.trans_no[WRD]=100;
// fp = fopen("testfile","a"
fwrite(&xy,sizeof(XYZ),1,fp);
fclose(fp);
FILE *fp1;
fp1 = fopen("testfile","r"
fread(&ab,sizeof(ABC),1,fp1);
printf("Hour is %u \n",ab.hr);
printf("Number is %d \n",ab.trans_no[WRD]);
// fclose(fp1);
// fp1 = fopen("testfile","r"
fread(&xy,sizeof(XYZ),1,fp1);
printf("Number from XYZ is %d \n",xy.trans_no[WRD]);
fclose(fp1);
}