c++ - XCode C++ 缺少精子 ()

标签 c++ xcode

我正在使用 C++ 和 XCode 创建一个 cmd 行应用程序来保存文件权限,但是我无法识别 sperm() 方法,错误是

'Use of undeclared identifier 'sperm'

我的包含和有问题的代码片段如下......

// My includes ...
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <vector>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdint.h>

// Code fragment ...

dp = opendir ("/var/someplace");
if (dp != NULL)
{
    while ((ep = readdir (dp)))
    {

        oFile = new FileObject;

        oFile->setName( ep->d_name );
        oFile->setIsDirectory( ep->d_type == isFolder );

        oFiles.push_back (*oFile);            
        // If it's a folder then we can get it's innards 

        if (stat(ep->d_name, &statbuf) == -1)
            continue;
        cout << "%10.10s", sperm(statbuf.st_mode);

        iFile++;
    }



    closedir (dp);
}
else
    perror ("Couldn't open the directory");

最佳答案

这可能让我看起来像个变态,但我在谷歌上搜索了“精子”(当然只搜索 .h 和 .cpp 文件)。 坏消息是我找不到对它的任何引用(除了在 stat function 页面本身)。

好消息是我找到了this定义它自己的“精子”功能的一段代码:

char const * sperm(__mode_t mode) {
    static char local_buff[16] = {0};
    int i = 0;
    // user permissions
    if ((mode & S_IRUSR) == S_IRUSR) local_buff[i] = 'r';
    else local_buff[i] = '-';
    i++;
    if ((mode & S_IWUSR) == S_IWUSR) local_buff[i] = 'w';
    else local_buff[i] = '-';
    i++;
    if ((mode & S_IXUSR) == S_IXUSR) local_buff[i] = 'x';
    else local_buff[i] = '-';
    i++;
    // group permissions
    if ((mode & S_IRGRP) == S_IRGRP) local_buff[i] = 'r';
    else local_buff[i] = '-';
    i++;
    if ((mode & S_IWGRP) == S_IWGRP) local_buff[i] = 'w';
    else local_buff[i] = '-';
    i++;
    if ((mode & S_IXGRP) == S_IXGRP) local_buff[i] = 'x';
    else local_buff[i] = '-';
    i++;
    // other permissions
    if ((mode & S_IROTH) == S_IROTH) local_buff[i] = 'r';
    else local_buff[i] = '-';
    i++;
    if ((mode & S_IWOTH) == S_IWOTH) local_buff[i] = 'w';
    else local_buff[i] = '-';
    i++;
    if ((mode & S_IXOTH) == S_IXOTH) local_buff[i] = 'x';
    else local_buff[i] = '-';
    return local_buff;
}

用法很简单:

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

int main(int argc, char ** argv)
{
    std::cout<<sperm(S_IRUSR | S_IXUSR | S_IWGRP | S_IROTH)<<std::endl;
    std::cout<<sperm(S_IRUSR)<<std::endl;
    std::cout<<sperm(S_IRUSR | S_IRGRP | S_IWOTH | S_IROTH)<<std::endl;
    return 0;
}

ideone 上输出:

r-x-w-r--
r--------
r--r--rw-

关于c++ - XCode C++ 缺少精子 (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9638714/

相关文章:

ios - 在 XCUITest 中单击按钮后如何验证和断言动画是否正在播放?

ios - cocoapods - 链接器命令失败,退出代码为 1

c++ - android ndk 和 cygwin

c++ - 二维碰撞 react

c++ - 使用 FMOD 和 MinGW 编译项目时出现很多错误

xcode - 使用 XcodeGen 进行自定义配置

c++ - 为类模板重载运算符<<

c++ - 在数组中查找最小值

ios - 如何禁用选项卡栏项目以及如何更改禁用项目的颜色和透明度

objective-c - 在 Xcode 中使用 Storyboard,如何在 iOS 中处理大量 Storyboard