XCode C++ missing sperm()
XCode C++ missing sperm()
我正在使用C++和XCode创建一个cmd行应用程序来保存文件权限,但我无法识别sperm()方法,错误为
'使用未申报的标识符'精子'
我的includes和有问题的代码如下。。。
// 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函数页本身)。
好消息是,我发现了一段定义其自身"精子"功能的代码:
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;
}
视频输出:
r-x-w-r--
r--------
r--r--rw-
我几年前就遇到过这种情况。我现在不想用那个特定的搜索词小心翼翼地在谷歌上搜索,但如果我没记错的话,答案是sperm()
是Solaris上可用的非标准系统功能。但是,由于它不是unix标准的一部分,您在OS X上找不到它。
假设函数已经定义(我不会在工作中用谷歌搜索这个名称),那么打印它的方式就有问题了:
cout << "%10.10s", sperm(statbuf.st_mode);
这不会打印格式化的字符串,因为C++iostream不像C的printf
那样工作。你可以不格式化它:
cout << sperm(statbuf.st_mode);
或使用printf
:
printf("%10.10s", sperm(statbuf.st_mode));
或者用iostream操纵器做一些小动作。
相关文章:
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决C++中声纳库贝静态代码分析错误"Explicitly define the missing copy constructor, move constructor .."
- luaL_len is missing in Lua 5.1
- 文件存在时的最新检查声明生成输入"is missing"
- Qt5PrintSupportd.dll is missing
- Missing sync_val_compare_and_swap_1
- 使用 dev-c++ 运行我的 GIS 应用程序可以"gdal201.dll is missing"
- Windows Boost 1.64 VS2017 missing Python libraries
- missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES
- CMake build error: Qt5PrintSupportConfig.cmake missing
- 如何修复错误消息,如"argument list for class template is missing"?
- "Missing GL version" 从 glewInit() 使用 EGL?
- cudart64_80.dll is missing
- C++ "argument list for class template "迭代器" is missing"错误
- LLONG_MAX missing for g++ 4.9.4?
- 交叉编译时"DSO missing from command line" 莫斯基托示例
- C++ 另一个标头中的结构函数给出"missing type specifier"
- setenv.bat is missing Windows
- XCode C++ missing sperm()