Click to See Complete Forum and Search --> : How to get file size?


Joshi
09-25-2000, 05:07 PM
How to get file size? something like:

FILE *fp;

fp = fopen(file_name, "a");
int lFileSize = get_file_size(fp);
...

please help.
Joshi

Wayne Mack
09-26-2000, 06:47 AM
Joshi,

Try using the _stat function, something like:

struct _stat theStatus;
if(_stat(yourFileNameHere, &theStatus) != 0)
{
theStatus.st_size = 0;
}
return(theStatus.st_size);

chris
09-26-2000, 09:01 AM
"Wayne Mack" <wmack@pec.com_nospam> wrote:
>
>Joshi,
>
>Try using the _stat function, something like:
>
> struct _stat theStatus;
> if(_stat(yourFileNameHere, &theStatus) != 0)
> {
> theStatus.st_size = 0;
> }
> return(theStatus.st_size);
>

although the above works, it is platform specific. if you care about such
things, a more standard method is to seek to the end of the file and get
the current position.

std::ifstream f(file_name.c_str());
if (f.is_open())
{
f.seekg(0, std::ios::end);
std::cout << file_name << " is " << f.tellg() << " bytes."
}