c - fread() returns incorrect data -
i trying read 128kb binary file in chunks of 256 bytes. first 20-40 bytes of 256 bytes seems correct. after data gets corrupted. tried reading file , writing binary file , compared. more half of data corrupted. here code
uint8_t buffer[256] read_bin_file = fopen("vtest.bin", "r"); if (read_bin_file == null) { printf("unable open file\n"); return false; } test_bin = fopen("test_file.bin", "w"); if (test_file == null) { printf("unable open file\n"); return false; } fflush(stdout); (i = 0; <=0x1ff; i++) { file_read_pointer = * 256; fseek(read_bin_file, file_read_pointer, seek_set); fread(buffer, 256, 1, read_bin_file); fseek(test_file, file_read_pointer, seek_set); fwrite(buffer, 256, 1, test_file); }
what missing? when try increase bytes read 256 1024 ( i<0x7f) error seems decrease significantly. file 90% matching
if binary data you're reading , writing, should open files in binary mode read_bin_file = fopen("vtest.bin", "rb");
. note "b" in mode. prevents special handling of new line characters.
your fseek
s unnecessary, fread
, fwrite
calls handle you. here "the file position indicator stream advanced number of characters read."
Comments
Post a Comment