7

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'

このスレッドから: C でファイルのサイズを取得するにはどうすればよいですか? このコードは機能すると思いますが、コンパイルできない理由がわかりません。誰かが私が間違っていることを見つけることができますか?

4

1 に答える 1

6

あなた\の は であるはず/ですか、それとも私があなたの環境について混乱しているだけですか?

UNIX MAN ページ:

 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>

 int stat(const char *restrict path, struct stat *restrict buf);

Windows を使用している場合\( stat.

于 2012-02-14T21:23:05.720 に答える