i'm a beginner in C and in my internship i have to work with a binary file and i must read the information in a binary file, one information at a time,
int main()
{
FILE *ptr=fopen("C:\\Users\\Workstation\\Desktop\\TEST.MAILO","rb");
char header[65];
char CRLF[3];
char first40[41];
char rio[33];
char date_capture[9];
if (ptr==NULL){
printf("Erreur");
exit(EXIT_FAILURE);
}
fread(header, sizeof(char), 64, ptr);
fread(CRLF, sizeof(char), 2, ptr);
fread(first40,sizeof(char),40,ptr);
fread(rio,sizeof(char),32,ptr);
fread(date_capture,sizeof(char),8,ptr);
printf("%s%s",header,CRLF);
printf("%s|||%s|||",first40,rio);
printf("%s||",date_capture);
return 0;
}
the problem is that in the last variable "date_capture" i ask for 8chars for the date but it gets more than 8
EIMG05050120170609007MADIMC0500014 0310000032050810011010358897200120007780|||007MAD00901020170609000109259941|||20170608■007MAD00901020170609000109259941||
it is supposed to print only 20170608 without the rest ■007MAD00901020170609000109259941|| please help me i looked everywhere i haven't found any problem like this. THANK YOU
fread
will not null terminate the buffer passed to it, you are seeing extra characters as the data in date_capture
is not null terminated, you should call memset
to set date_capture
to all zeros prior to passing it to fread