fread() questions.

This is a discussion on fread() questions. within the C Programming forums, part of the General Programming Boards category; Hey guys, im reading a binary file and then using fprintf to write the data to a text file. the ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    fread() questions.

    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:
    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
    when reading an integer, does fread() move the file offset by 1 or by the size of the int (4bytes on my machine).
    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

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Do you NULL terminate the array string[] after reading characters from the binary file into it?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    note that ftell returns long - so better adjust the type of the variable that stores the result

    and you actually do not need it (it could fail on very long files) - just check the return value of each fread...

    also your function name is confusing - you calling it write something, while actually reading something
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 01:45 PM
  2. using fread on stdin
    By nadroj in forum C Programming
    Replies: 29
    Last Post: 10-23-2008, 03:03 PM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2007, 12:25 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21