【问题标题】:Get File size from CFileDialog从 CFileDialog 获取文件大小
【发布时间】:2015-07-27 02:04:14
【问题描述】:

我是 Visual Studio C++ 的新手。我正在使用 CFileDialog 从用户输入中获取文件名和文件路径。现在我想使用正在加载过程的进度控制,用户必须等待取决于您的输入文件大小。现在我通过使用 CFileDialog 获得了文件名和文件路径,但我不知道如何获取用户输入文件的大小。

我使用下面的方法,它总是返回零。

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

如果您有更好的解决方案来获取文件大小,请建议我。

非常感谢。

【问题讨论】:

标签: c++ visual-studio-2013 cfiledialog


【解决方案1】:

标准的便携方式是:

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; 

本机 Windows 方法是使用 GetFileSize()

但是,如果您查看不首先打开文件的 MFC 替代方案,您可能会查看 this SO question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-17
    • 2017-08-26
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2013-08-10
    • 1970-01-01
    相关资源
    最近更新 更多