【发布时间】:2012-03-06 06:08:44
【问题描述】:
我对 stat.h 有一个非常奇怪的问题
在我的代码顶部,我有声明:
#include <sys\types.h>
#include <sys\stat.h>
以及函数原型:
int FileSize(string szFileName);
最后,函数本身定义如下:
int FileSize(string szFileName)
{
struct stat fileStat;
int err = stat( szFileName.c_str(), &fileStat );
if (0 != err) return 0;
return fileStat.st_size;
}
当我尝试编译这段代码时,我得到了错误:
divide.cpp: In function 'int FileSize(std::string)':
divide.cpp:216: error: aggregate 'stat fileStat' has incomplete type and cannot be defined
divide.cpp:217: error: invalid use of incomplete type 'struct stat'
divide.cpp:216: error: forward declaration of 'struct stat'
来自这个帖子:How can I get a file's size in C? 我认为这段代码应该可以工作,但我不知道为什么它不能编译。谁能发现我做错了什么?
【问题讨论】:
-
我遇到了同样的问题,尽管我使用了正确的路径分隔符。我在 Windows 上使用 MinGW 进行编译,编译器有 sys/stat.h 文件,其中声明了 struct stat 和 function stat 。还有什么想法吗?
标签: c++ filesize stat incomplete-type