Hi I am trying to read a binary file and process it but I think I am using fread wrong way, when I try to use fread the no. of bytes read are less then the size of file. can anyone help me, white I am doing wrong
#include <stdio.h>
#include <limits.h>
int main()
{
FILE *fin=fopen("./file.pcap","rb");
char line[LINE_MAX];
FILE *fout=fopen("out.txt","w");
while(fread(line,sizeof(line),1,fin)){
fwrite(&line,sizeof(line),1,fout);
}
fclose(fin);
fclose(fout);
}
The first file is around 51236 and out.txt is 51200
Fread returns the number of “elements” read. You have said an element is
LINE_MAXbytes long, so when you get to the end of the file, there is not a full element for you, sofread()returns 0 and your output ends up truncated.try flipping to read “a number of bytes” rather than “1 block of LINE_MAX bytes”: