Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > FileSize() function

Reply
Thread Tools

FileSize() function

 
 
christopher diggins
Guest
Posts: n/a
 
      12-10-2004
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:os_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


 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      12-10-2004

"christopher diggins" <(E-Mail Removed)> wrote in message
news:H%aud.28409$(E-Mail Removed).. .
> 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....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


 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      12-10-2004
"christopher diggins" <(E-Mail Removed)> 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:os_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.


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      12-10-2004
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:os_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

 
Reply With Quote
 
christopher diggins
Guest
Posts: n/a
 
      12-10-2004
I appreciate the help, thanks all!

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com


 
Reply With Quote
 
christopher diggins
Guest
Posts: n/a
 
      12-10-2004
I used the information shared here to write a mini-article on the subject of
FileSize at CodeProject.com
http://www.codeproject.com/useritems/filesize.asp
as the first installment on a series on tips, tricks and hacks called the
Quick and Dirty Series.

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com


 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      01-10-2005

"christopher diggins" <(E-Mail Removed)> wrote in message news:H%aud.28409$(E-Mail Removed).. .
> 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:os_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[i] << " ";
}
cout << endl;
cout << endl;


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

return 0;
}



--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments