如何使用C++在 Linux 中获取文件的所有者名称?
How to get file's owner name in Linux using C++?
如何使用c++获取Linux文件系统上文件的所有者名称和组名称?stat()
调用只给我所有者ID和组ID,而不是实际的名称。
-rw-r--r--. 1 john devl 3052 Sep 6 18:10 blah.txt
如何以编程方式获得'john'和'devl' ?
使用getpwuid()
和getgrgid()
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
struct stat info;
stat(filename, &info); // Error check omitted
struct passwd *pw = getpwuid(info.st_uid);
struct group *gr = getgrgid(info.st_gid);
// If pw != 0, pw->pw_name contains the user name
// If gr != 0, gr->gr_name contains the group name
一种方法是使用stat()
获取文件的uid,然后使用getpwuid()
获取用户名作为字符串。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- ERROR_ACCESS_DENIED设置文件所有者
- 在写入远程Windows共享文件夹的所有者时,GetNamedSecurityInfo返回ERROR_ACCESS_DE
- 如何使用 WinAPI c++ 获得文件所有者
- 如何使用 c++ 和 windows api 获取 Windows 文件"author"而不是所有者
- 如何使用C++在 Linux 中获取文件的所有者名称?