我正在對目錄中的所有文件進行簡單測試。 但是由於某種原因,有時候,他們的行爲是錯誤的? 我的代碼有什麼不好?測試目錄S_ISDIR行爲不一致
using namespace std;
int main() {
string s = "/home/";
struct dirent *file;
DIR *dir = opendir(s.c_str());
while ((file = readdir(dir)) != NULL){
struct stat * file_info = new (struct stat);
stat(file->d_name,file_info);
if ((file_info->st_mode & S_IFMT) == S_IFDIR)
cout << "dir" << endl;
else
cout << "other" << endl;
}
closedir(dir);
}
請解釋您的意思是「行爲錯誤」。是否有目錄輸出爲「other」,或其他輸出爲「dir」的東西? – 2010-05-15 20:32:18
@Roland Illig:都 – coubeatczech 2010-05-15 22:46:35