Size of File in C
Results 1 to 2 of 2

Thread: Size of File in C

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    420

    Size of File in C

    Hi All,

    I recently created a program that replaces the contents of a file with different contents. One of my requirements was that the file was same size when I was done. Coding in C the file size is handled by the system. On google I found links to truncate a file but nothing telling me how to get the size.

    I tried size = sizeof (filepointer); where filepoint is of type FILE* and the file has been opened but that returns 8. I tried sizeof (pathtofile) but that returns the size of the path. What I ended up doing was the following:

    int FileSize(FILE *index)
    {
    int size = 0;
    while (!feof(index)){
    fgetc(index);
    size ++;
    }//close while
    return size;

    }//close FileSize

    I was wondering if there is a more eloquent way to achieve the same result. Any suggestions would be greatly appreciated. It should return size - 1 (I handle that later in the program).

    Thanks,
    -D

    program built using vc++ from visual studio 6
    If you spend more on coffee than on IT security, you will be hacked. What\'s more, you deserve to be hacked.
    -- former White House cybersecurity adviser Richard Clarke

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Hi. If you can use C++, read my post in this thread:
    http://www.antionline.com/showthread...hreadid=255646

    If you have to use C, replace seekg() with fseek() and tellg() with ftell() in the example in my post.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •