English 中文(简体)
测试目录 S_ISDIR 行为不一致
原标题:testing directory S_ISDIR acts inconsistently
  • 时间:2010-05-15 19:56:00
  •  标签:
  • c++
  • file

I m doing simple tests on all files in directory. But from some reason, sometimes, they behave wrongly? What s bad with my code?

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);
}
最佳回答

你犯了一些错误,其中最重要的是在不核对其收益价值的情况下打上stat(>>。 我修改了你的方案:

#include <cstdio>
#include <dirent.h>
#include <iostream>
#include <string>
#include <sys/stat.h>

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;

    if (stat(file->d_name, &file_info) == -1) {
      perror("stat");
      return 1;
    }

    if (S_ISDIR(file_info.st_mode))
      cout << "dir " << file->d_name << endl;
    else
      cout << "other " << file->d_name << endl;
  }
  closedir(dir);
}

我拿着这一结果:

$ ./a.exe
dir .
dir ..
stat: No such file or directory

现在,我看见,stat号>的档案名称为roland,而目前的工作名录中确实有这种编号。 你必须用名录预先确定档案名称。

您的第二点建议是,每次分配一个新的<代码> stat,但不得在使用后释放记忆。 否则,C++就没有收集垃圾,因此,你的方案很快就会消失。

问题回答

如果只设定<代码>S_IFD,你可以尝试检查:

if((statbuf.st_mode & S_IFDIR)  == S_IFDIR)   
{//dir
}

我看到以下定义:

#define _S_IFMT         0xF000          /* file type mask */
#define _S_IFDIR        0x4000          /* directory */
#define _S_IFCHR        0x2000          /* character special */
#define _S_IFIFO        0x1000          /* pipe */
#define _S_IFREG        0x8000          /* regular */
#define _S_IREAD        0x0100          /* read permission, owner */
#define _S_IWRITE       0x0080          /* write permission, owner */
#define _S_IEXEC        0x0040          /* execute/search permission, owner */

我不肯定你的情况,但S_IFDIR不是在“<0xF000上设定或超过其中一项。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签