Thread: Cannot read correct file size

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    25

    Cannot read correct file size

    For some reason whenever I read a file it always adds garbage add the end. I can't figure out why.

    Code:
    fseek(f, 0, SEEK_END);
    long s = ftell(f);
    rewind(f);
    fbuffer = malloc(s);
    fread(fbuffer, 1, s, f);
    fclose(f);
    Always the "s" is too large.

    Can anyone tell me why or how to correctly read files.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What OS are you using?
    How did you open the file?
    By how much is the file size off?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Windows XP/7.

    Its a regular text file so "f = fopen("textfile.txt", "r");".

    Its always off by 1-7 maybe 1-10bytes.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Like by maybe 1 to 7 carriage returns worth? Don't fseek text files.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    It still should work properly.

    And if I don't use it, what else will I use to read it correct?

  6. #6
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    Use stat.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Your buffer variable is not null-terminated, so if you're trying to print it/treat it as a string, then there are your extra characters...you're printing random data until a '\0' is found in memory. Don't forget to malloc enough space for that null-terminator you need.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is a pathetically sad thing about windows. Each line in a text file actually ends with \r\n. But that is translated to just \n when it's read in text mode. So you will have one extra byte per line.

    The fact that you're malloc'ing a few extra bytes is not a big problem. The problem is with properly terminating the buffer. To do so you could use the return value of fread.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        char*  fbuffer;
        FILE*  f;
        long   s;
        size_t st;
    
        f = fopen("atextfile.txt", "r");
        fseek(f, 0, SEEK_END);
        s = ftell(f);
    
        fbuffer = malloc(s + 1);
    
        rewind(f);
        st = fread(fbuffer, 1, s, f);
        fbuffer[st] = 0;
        fclose(f);
    
        printf("s=%ld\nst=%u\nText:\n%s", s, st, fbuffer);
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocated memory size is not correct.
    By ramtinraji in forum C Programming
    Replies: 8
    Last Post: 02-22-2011, 11:16 PM
  2. how to i correct this code to read the file correctly?
    By funit49 in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2008, 07:56 PM
  3. is this the correct/best way to set file size?
    By George2 in forum C Programming
    Replies: 11
    Last Post: 07-18-2006, 09:07 AM
  4. Replies: 1
    Last Post: 05-26-2004, 12:58 AM