#include <stdio.h> //#pragma pack(1) 프라그마 쓰면 1byte트씩 하나씩하나씩 차곡차곡 들어가는거 아시져? struct smart { int iNum; char A; int iNum2; char B; short C; }; //#pragma pack(4)
int main() { // int iNum=0x41424344; struct smart test; struct smart test2;
FILE * fp; fp = fopen("AA.bin","wb");
test.iNum = 0x41424344; test.A = 'Z'; test.iNum2 = 0x45464748; test.B = 'Y'; test.C = 0x494A;
// fwrite(&iNum,4,1,fp); fwrite(&test,sizeof(struct smart),1,fp); //혹은 sizeof(test)로 해도됨 fclose(fp);
fp = fopen("AA.bin","rb"); fread(&test2,sizeof(struct smart),1,fp); //fp를 읽어서 fclose(fp); printf("struct smart'size: %d \n", sizeof(struct smart)); 스마트구조체 크기
printf("%08X \n", test2.iNum); printf("%c \n", test2.A); printf("%08X \n", test2.iNum2); printf("%c \n", test2.B); printf("%08X \n", test2.C);
return 0; }
|
RECENT COMMENT