Hey guys,
im reading a binary file and then using fprintf to write the data to a text file.
the sequence of each line is first, an (unsigned char) that determines the length of the string that follows, the string itself, and an integer right after it.
My file is supposed to look like this:
a 74
abc 973
defghij 0
input.dat 1
output.dat 2745
ThisIsaLongStringAndItIsDeliberatelySo 36745
ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX YZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV WXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst uvwxyabcdefghijklmnopqrstuvwxyabcdefghijklmnopqrst uvwxyabcdefghijklmnopqrstuvwxyabcdefghijklmnopqrst uvwxy 123456
but instead I get:
a 74
abcÿ 973
defghij 0
input.dat 1
output.dat 2745
ThisIsaLongStringAndItIsDeliberatelySoã¨ÿ¿ù€ÿ*ˆ.. 36745
ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX YZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV WXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst uvwxyabcdefghijklmnopqrstuvwxyabcdefghijklmnopqrst uvwxyabcdefghijklmnopqrstuvwxyabcdefghijklmnopqrst uvwxy 123456
which is almost identical except from line 2 and 6 (i made bold for emphasis). The integers are correct which makes it more difficult to understand where the error is.
here is my code:
when reading an integer, does fread() move the file offset by 1 or by the size of the int (4bytes on my machine).Code:void writeTextFile(FILE *finp, FILE *fop){ char string[256]; int fileLen, theinteger, pos; unsigned char num1; fseek(finp, 0L, SEEK_END); fileLen = ftell(finp); //highest offset rewind(finp); while(pos < fileLen){ //do while offset is less than fileLen fread(&num1, sizeof(unsigned char), 1, finp); //num1 determines how long the string that follows is fread(string, sizeof(char), num1, finp); //read the string fprintf(fop, "%s\t", string); //print the string fread(&theinteger, sizeof(int), 1, finp); //read the integer that comes right after the string fprintf(fop, "%d\n", theinteger); //print the integer pos = ftell(finp); }//end while }//end function
I asume that fread moves the file offset by n when reading a string of size n.
Why does my code have extra junk in it but at the same time it doesnt effect the reading of other elements in the file.
Any thoughts?
Thanks



LinkBack URL
About LinkBacks


