Thursday, July 24, 2008

Calculate File size with C++

Programming .....

  The following is a C++ program to calculate file size of a file. It has been tested to correctly report file sizes upto 4Gb, ofcourse used 32 bit compiler!

--Begin--

/* getFileSize.cpp
 * Gets the file size of a DiskFile
 *
 * Midhun Hk (Centrum inc Software Solutions)
 * July 2008
 * $ v 0.1
 **/

# inculde <ifstream>
# include <iostream>

void main()
{
    ifstream is;
    unsigned long fileSize = 0L;

    is.open("FileName.bin");   // Open the file
    if(is.good())                    // If file is opened successfully
    {
           // calculate FileSize
           is.seekg(0,ios::end); // move 0 bytes from end
           fileSize = is.tellg();   // get offset
           is.seekg(0,ios::beg); // rewind
     }

      cout<<"File Size is : "<<fileSize<<:" bytes";
}

--End--

Hope this has been useful.

No comments: