FileSize() function

Discussion in 'C++' started by christopher diggins, Dec 10, 2004.

  1. Is there a simpler way to query the size of a file in C++ other than the
    following:

    unsigned int FileSize(const char* sFileName)
    {
    std::ifstream f;
    f.open(sFileName);
    if (!f.Good()) { return 0; }
    std::ifstream::pos_type begin_pos = f.tellg();
    f.seekg(0, std::ios_base::end);
    return f.tellg() - begin_pos;
    }

    TIA

    --
    Christopher Diggins
    http://www.cdiggins.com
    http://www.heron-language.com
    christopher diggins, Dec 10, 2004
    #1
    1. Advertising

  2. "christopher diggins" <> wrote in message
    news:H%aud.28409$...
    > Is there a simpler way to query the size of a file in C++ other than the
    > following:


    Hi There,

    Boost.Filesystem now provides a function for querying file size.

    http://www.boost.org/libs/filesystem/doc/operations.htm#file_size

    It's superior to seeking within a file because it doesn't open the file and may
    support larger file sizes that can be represented by std::streamoff.

    Jonathan
    Jonathan Turkanis, Dec 10, 2004
    #2
    1. Advertising

  3. christopher diggins

    Siemel Naran Guest

    "christopher diggins" <> wrote in message
    news:H%aud.28409

    > unsigned int FileSize(const char* sFileName)
    > {
    > std::ifstream f;
    > f.open(sFileName);
    > if (!f.Good()) { return 0; }
    > std::ifstream::pos_type begin_pos = f.tellg();
    > f.seekg(0, std::ios_base::end);
    > return f.tellg() - begin_pos;
    > }


    The standard allows that tellg returns zero every time, so this method is
    not full garaunteed, technically speaking.
    Siemel Naran, Dec 10, 2004
    #3
  4. christopher diggins wrote:
    > Is there a simpler way to query the size of a file in C++ other than the
    > following:
    >
    > unsigned int FileSize(const char* sFileName)
    > {
    > std::ifstream f;
    > f.open(sFileName);
    > if (!f.Good()) { return 0; }
    > std::ifstream::pos_type begin_pos = f.tellg();
    > f.seekg(0, std::ios_base::end);
    > return f.tellg() - begin_pos;
    > }
    >
    > TIA
    >


    To obtain the exact size of a file, one must use
    an operating system function. There is no guaranteed
    standard method (that works on all platforms) for
    obtaining the file size. Search newsgroups
    news:comp.lang.c and news:comp.lang.c++ for discussions
    on this topic.

    Some of the pitfalls:
    1. The size of the file may be larger than the number
    of characters in the file.
    2. The size of the file may be larger than the number
    of binary units (bytes, words, etc.) inside it.
    3. When counting characters, the O.S. may translate
    characters, such as carriage return and linefeed
    into a single newline character.
    4. The size of the file may be larger then the capacity
    returned by tellg.

    Most applications avoid obtaining the file size and just
    read until the end of the file. As for reading a file
    into memory, the safe technique is to allocate some N
    units of memory and use a block read into memory, and
    note the amount of characters read. Repeat until
    all characters in the file have been read.


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Dec 10, 2004
    #4
  5. christopher diggins, Dec 10, 2004
    #5
  6. christopher diggins, Dec 10, 2004
    #6
  7. christopher diggins

    Alex Vinokur Guest

    "christopher diggins" <> wrote in message news:H%aud.28409$...
    > Is there a simpler way to query the size of a file in C++ other than the
    > following:
    >
    > unsigned int FileSize(const char* sFileName)
    > {
    > std::ifstream f;
    > f.open(sFileName);
    > if (!f.Good()) { return 0; }
    > std::ifstream::pos_type begin_pos = f.tellg();
    > f.seekg(0, std::ios_base::end);
    > return f.tellg() - begin_pos;
    > }
    >
    > TIA
    >
    > --
    > Christopher Diggins
    > http://www.cdiggins.com
    > http://www.heron-language.com
    >
    >



    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <limits.h>
    #include <assert.h>
    using namespace std;

    typedef unsigned long long ullong;

    #define MAX_FILENAME_LEN 20
    #define MAX_FILESIZE_LEN ((sizeof(ullong) * CHAR_BIT) - 2)
    #define MAX_FILESIZE_VALUE (static_cast<ullong>(1) << (MAX_FILESIZE_LEN))
    #define SETW_FILESIZE_LEN 15

    // --------------------------------------
    bool get_file_size(
    const string& filename_i,
    ullong& filesize_o
    )
    {
    ifstream fin (filename_i.c_str());
    if (fin.fail()) return false;

    ullong start_pos, end_pos;
    start_pos = fin.tellg();
    fin.seekg(0, ios::end);
    end_pos = fin.tellg();

    filesize_o = end_pos - start_pos;
    assert (filesize_o < MAX_FILESIZE_VALUE);

    return true;

    } // get_file_size


    // --------------------------------------
    void show_file_size(
    const string& filename_i
    )
    {
    ullong filesize;

    cout << setw (MAX_FILENAME_LEN) << filename_i << " : ";
    if (get_file_size (filename_i, filesize))
    {
    cout << setw (SETW_FILESIZE_LEN) << filesize << " bytes";
    }
    else
    {
    cout << "Unable to get file size";
    }
    cout << endl;

    } // show_file_size


    // --------------------------------------
    int main (int argc, char** argv)
    {
    cout << "### Your input is : ";
    for (int i = 0; i < argc; i++)
    {
    cout << argv << " ";
    }
    cout << endl;
    cout << endl;


    for (int i = 1; i < argc; i++)
    {
    show_file_size(argv);
    }

    return 0;
    }



    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Jan 10, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen

    Format filesize in kB or MB

    Arjen, Aug 8, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,119
    Jerry III
    Aug 9, 2003
  2. David Colliver

    Problem retrieving filesize

    David Colliver, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    372
    David Colliver
    May 17, 2005
  3. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    Response.writefile (filename) ?? problem with filesize

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Aug 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    497
    Buddy Ackerman
    Aug 16, 2005
  4. VB Programmer

    How check filesize using FileUpload control?

    VB Programmer, Jan 18, 2006, in forum: ASP .Net
    Replies:
    11
    Views:
    20,357
  5. Michel Rosien

    STL Stream maximum filesize?

    Michel Rosien, Sep 18, 2003, in forum: C++
    Replies:
    8
    Views:
    615
    Mike Wahler
    Sep 18, 2003
Loading...

Share This Page