Your fread
is writing into a buffer of longs; so you'd expect only 3.5 million (= 14 million / sizeof(long)) items to be written; but you're accessing buffer as if you expect it to be a character array. Accessing the 3.5+ millionth item walks off the end of the array, since you malloc'ed it to be 14 million bytes, not 14 million entries.
In addition, fread
never puts EOF into the result it writes into the buffer; it writes out the contents of the file unchanged. You must look at the return value of fread
to see how much was actually read.