C++ stat.h 类型不完整且无法定义

标签 c++ filesize stat incomplete-type

我遇到了一个非常奇怪的 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/

相关文章:

php循环文件夹获取文件名和大小

c - 程序集 x86 从任何文件读取并转义所有特殊字符并获取文件大小(以字节为单位)

c++ - Windows下的_stat()到底是怎么工作的

c++ - WAV 文件的 MP3 编解码器

python-3.x - 为什么 Python 找到与 Windows 不同的文件大小?

c - stat()在c中给出错误的目录大小

c - vfs_stat() 调用有什么问题?

c++ - 错误 'operator*' 不匹配

c++ - 分组网格上的食人魔碰撞检测?

c++ - 如何在 nlohmann::json 文件 C++ 中删除项目内的项目