我遇到了一个非常奇怪的 stat.h 问题
在我的代码顶部,我有声明:
#include <sys\types.h>
#include <sys\stat.h>
和函数原型(prototype):
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? 我认为这段代码应该可以工作,但我不明白为什么它不能编译。任何人都可以发现我做错了什么吗?
最佳答案
您的 \
应该是 /
还是我只是对您的环境感到困惑?
UNIX 手册页:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *restrict path, struct stat *restrict buf);
如果您使用的是 Windows(我猜您可能是因为 \
的缘故),那么我也无能为力,因为我什至不知道它有 >统计
。
关于C++ stat.h 类型不完整且无法定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9284258/