使用C++和stat获取所有者的访问权限

pyp*_*ies 2 c++ linux file mode stat

如何使用Kubuntu Linux中的C++使用sys/stat.h中的stat获取文件所有者的访问权限?

目前,我得到这样的文件类型:

  struct stat results;  

  stat(filename, &results);

  cout << "File type: ";
  if (S_ISDIR(results.st_mode))
    cout << "Directory";
  else if (S_ISREG(results.st_mode))
    cout << "File";
  else if (S_ISLNK(results.st_mode))
    cout << "Symbolic link";
  else cout << "File type not recognised";
  cout << endl;
Run Code Online (Sandbox Code Playgroud)

我知道我应该使用t_mode的文件模式位,但我不知道如何.请参见sys/stat.h

Gun*_*iez 7

  struct stat results;  

  stat(filename, &results);

  cout << "Permissions: ";
  if (results.st_mode & S_IRUSR)
    cout << "Read permission ";
  if (results.st_mode & S_IWUSR)
    cout << "Write permission ";
  if (results.st_mode & S_IXUSR)
    cout << "Exec permission";
  cout << endl;
Run Code Online (Sandbox Code Playgroud)