I have searched the messageboard for a while but didn't find anything similar.
I have written the following c prog:
Code:
#include <stdio.h>
#define RECSIZE 8+21+13+21+2*sizeof(int)+sizeof(char)
typedef struct {
char id[8];
char surname[21];
char name[13];
char street[21];
int number;
int postcode;
char condition;
} pelatis;
int main() {
FILE *fp1=fopen("dataset1.in","r");
pelatis p,p1;
fscanf(fp1,"%s %s %s %s %d %d %c",p.id,p.surname,p.name,p.street,&p.number,&p.postcode,&p.condition);
FILE *fp2=fopen("testfile","wb+");
fwrite(&p,RECSIZE,1,fp2);fseek(fp2,0,SEEK_SET);
fread(&p1,RECSIZE,1,fp2);
printf("%s %s %s %s %d %d %c\n",p1.id,p1.surname,p1.name,p1.street,p1.number,p1.postcode,p1.condition);
fclose(fp1);
fclose(fp2);
}
The dataset1.in is a text file with records such as:
A250000 OF PMJXVTEWS HACJDEZ 36 50111 0
A249999 JN PPERLJB UUNHWSYY 43 58540 0
A249998 WFUVZZ KGHVAJC BAXHHPRVS 62 53643 1
A249997 LYTCPKTPO DFLUN TIERMBSNDYDX 91 72358 0
A249996 ZRD VPEEVMENT HORTSM 111 21071 1
What I do is to read the first record,write it in the binary file testfile, and then read it and print it to validate the content I previously wrote.
All the fileds are printed as they should except for the condition field which is a single character.What is printed is:
Code:
A250000 OF PMJXVTEWS HACJDEZ 36 50111
If I change it to int and change the %c to %d,the condition field is printed correctly.
What am I doing wrong?
Thanks in advance.