FileSize() function

  • Thread starter christopher diggins
  • Start date
C

christopher diggins

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
 
S

Siemel Naran

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.
 
T

Thomas Matthews

christopher said:
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
and 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
 
A

Alex Vinokur

christopher diggins said:
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


#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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top