See the program below and see the output.
#include <stdio.h>
#include <string.h>
#define SIZENUMBER 15
typedef unsigned char byte;
typedef struct
{
byte trans_no[SIZENUMBER];
byte s_no[2];
byte abc;
}XYZ;
main()
{
XYZ xy;
FILE *fp,*fp1;
memcpy(xy.trans_no,"M02031315A0008",15);
xy.s_no[2] = 12;
xy.abc = 11;
printf("Number is %s \n",xy.trans_no);
printf("Number 2 is %d \n",xy.s_no[2]);
printf("Number 3 is %u \n",xy.abc);
fp = fopen("testfile","w"
fwrite(&xy,sizeof(XYZ),1,fp);
fclose(fp);
fp1 = fopen("testfile","r"
fread(&xy,sizeof(XYZ),1,fp1);
printf("Number from XYZ is %s \n",xy.trans_no);
printf("Number 2 XYZ is %d\n",xy.s_no[2]);
printf("Number 3 XYZ is %d\n",xy.abc);
fclose(fp1);
}
I am getting the output as
Number is M02031315A0008
Number 2 is 1
Number 3 is 1
Number from XYZ is M02031315A0008
Number 2 XYZ is 1
Number 3 XYZ is 1
though i should get the values of s_no as
12.
Is there anything wrong in the way i am filling these structures.I need to have all these varieties of elements in my program.
Thanks
#include <stdio.h>
#include <string.h>
#define SIZENUMBER 15
typedef unsigned char byte;
typedef struct
{
byte trans_no[SIZENUMBER];
byte s_no[2];
byte abc;
}XYZ;
main()
{
XYZ xy;
FILE *fp,*fp1;
memcpy(xy.trans_no,"M02031315A0008",15);
xy.s_no[2] = 12;
xy.abc = 11;
printf("Number is %s \n",xy.trans_no);
printf("Number 2 is %d \n",xy.s_no[2]);
printf("Number 3 is %u \n",xy.abc);
fp = fopen("testfile","w"
fwrite(&xy,sizeof(XYZ),1,fp);
fclose(fp);
fp1 = fopen("testfile","r"
fread(&xy,sizeof(XYZ),1,fp1);
printf("Number from XYZ is %s \n",xy.trans_no);
printf("Number 2 XYZ is %d\n",xy.s_no[2]);
printf("Number 3 XYZ is %d\n",xy.abc);
fclose(fp1);
}
I am getting the output as
Number is M02031315A0008
Number 2 is 1
Number 3 is 1
Number from XYZ is M02031315A0008
Number 2 XYZ is 1
Number 3 XYZ is 1
though i should get the values of s_no as
12.
Is there anything wrong in the way i am filling these structures.I need to have all these varieties of elements in my program.
Thanks