Search code examples
c++visual-studio-2013cfiledialog

Get File size from CFileDialog


I am newbie from Visual Studio C++. I am using CFileDialog to get the file name and file path from user input. and now i want to use progress control that is loading process and user have to wait depend on your input file size. Now I got the file name and file path by using CFileDialog but I don't know how to get a user input file size.

I am using below method and it always return zero.

int FileSize(const char * szFileName)
{
struct stat fileStat;
int err = stat(szFileName, &fileStat);
if (0 != err) 
    return 0;
return fileStat.st_size;
}

Please suggest me if you have a better solution to get a file size.

Thanks you very much.


Solution

  • The standard portable way to do it would be:

    long long sz;   // int would be to small for many files ! 
    ifstream ifs(test);
    if(!ifs) 
        return 0;   // when file couldn't be opened
    ifs.seekg(0, ios::end);   
    sz = ifs.tellg();
    return sz; 
    

    The native windows approach would be to use GetFileSize().

    But if you look at an MFC alternative that doesn't open the file first, you may look at this SO question.