Ansi C - check file size in bytes.

This is a discussion on Ansi C - check file size in bytes. within the C Programming forums, part of the General Programming Boards category; Iīve tried stat to get the file size of a certain file. When i run debugger i can see the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    Ansi C - check file size in bytes.

    Iīve tried stat to get the file size of a certain file.
    When i run debugger i can see the st_size field with the desired size, but i cant copy it to any other variable. iīve tried copying to int, unsigned long, etc, and the value always getīs corrupted.
    Can this be done or is there a more simple way to get a filesize? (ansi C)

    Thank You

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Sure, open the file ("rb"), go to the end with fseek() and find out where you are with ftell().

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is how you do it using standard libraries. st_size sounds like the stat() function in Unix/Linux.
    Code:
    long size;
    FILE *f = fopen("filename", "rb");
    if (f)
    {
       fseek(f, 0 SEEK_END);
       size = ftell(f);
       fclose(f);
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    i assume you forgot a "," between o and seek: fseek(f, 0 SEEK_END);

    i've tried that method but get a Program received signal SIGSEGV, Segmentation fault., in the fseek line

    FILE *fp;
    fp=open(blkname,O_RDONLY,0777);
    fseek(fp, 0, SEEK_END);
    size = ftell(fp);

    fp returns a valind number (7);
    also tried opening file with O_RDWR, and got same error.
    blkname is a file with read write permissionas to everyone
    i use open since itīs a binary file

    any toughts?
    Last edited by Ironic; 04-02-2009 at 07:32 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's because you are using open(), not fopen(). open returns a (most likely small) integer value, which is not a valid pointer (but not zero). Then fseek() will try to use that as a pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    fp=fopen(blkname,"rb");

    also tried this. same error

    with fopen, fp gets the same result

    in both cases if i do fileno(fp), it crashes, but donīt why or how to fix it.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    and is fp != NULL at this point?

    What is "blkname"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    fp is 0x7

    i can even read from the file

    but as soon i hit fseek it crashes...

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    if fp is 0x7, then it is NOT a FILE *, it's a filehandle. I bet that you are reading from the file with read(), not fread(). These are two different sets of file interfaces, one using an integer [essentially, and index into a table], the other using a POINTER to a FILE object.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    problem solved. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 08:45 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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