Getting a file size.

  • Thread starter Thread starter Abubakar
  • Start date Start date
A

Abubakar

Hi ,
I wrote the following code in c++ (native) to get file size:

int o_get_file_length(const char * filename)
{
ifstream f (filename, ios::binary );
f.seekg (0, ios::end );
int length = f.tellg ();
f.close();
return length;

}

Is this ok, or is there a better way to get the file size?

Regards,

-Ab.
 
Hi ,
I wrote the following code in c++ (native) to get file size:

int o_get_file_length(const char * filename)
{
ifstream f (filename, ios::binary );
f.seekg (0, ios::end );
int length = f.tellg ();
f.close();
return length;

}

You could use the stat or _fstat functions.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Win32 API (although I know I should be talking about .NET here) has a
GetFileSize API. The one advantage of this is that it is capable of
reporting file sizes larger than 4Gb, because it uses two DWORDs for the
result. If you're not dealing with huge files then this is not an issue of
course, and I'd recommend stat as previously advised.

Kevin
 
Thanks for the reply to all.
Win32 API (although I know I should be talking about .NET here) has a
I'm doing it in native pure c++, not using .net anywhere.
GetFileSize API. The one advantage of this is that it is capable of

Thanks for the winapi too.

Abubakar.
 
Back
Top