如何在Linux中使用C ++获取文件的所有者名称? [英] How to get file's owner name in Linux using C++?
本文介绍了如何在Linux中使用C ++获取文件的所有者名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用C ++获取Linux文件系统上文件的所有者名称和组名称? stat()
呼叫仅提供了所有者ID和组ID,但没有提供实际名称.
How can I get get the owner name and group name of a file on a Linux filesystem using C++? The stat()
call only gives me owner ID and group ID but not the actual name.
-rw-r--r--. 1 john devl 3052 Sep 6 18:10 blah.txt
如何以编程方式获取"john"和"devl"?
How can I get 'john' and 'devl' programmatically?
推荐答案
使用 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
这篇关于如何在Linux中使用C ++获取文件的所有者名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文