c - 使用 readdir_r 读取目录中的文件并使用 qsort 排序

标签 c struct qsort readdir getpwuid

我正在尝试用 C 语言编写一个程序,该程序从目录中读取文件并检测每个文件的名称、用户、组和大小。每个文件的信息存储在结构体数组中,并使用 qsort 按文件名排序。然后将排序后的文件打印到屏幕上。该程序必须使用readdir_r、getpwuid_r 和getgrgid_r。我不明白必须如何实现必须为这些函数的“_r”版本提供的多个参数才能实现我的目标。我还收到几个“请求非结构或 union 中的成员‘名称’”错误(也发生在“大小”、“用户”和“组”中)。

谁能帮助我理解在这种情况下如何正确使用“_r”函数?手册页不够清晰,我无法理解。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <stddef.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>

int main(int argc, char* argv[])
{

DIR *mydir;
struct dirent *myfile;
struct stat mystat;
struct passwd  *pwd;
struct group   *grp;
struct dirent *result;


struct entry
{
    char *name;
    char *user;
    char *group;
    int size;
};

struct entry entries[1024];

if(argc != 2)
{
    perror("must supply a directory");
    return -1;
}

mydir = opendir(argv[1]);
if(mydir==NULL)
{
    perror("Cannot find directory");
    return -1;
}

char buf[1024];
while((myfile = readdir_r(mydir, myfile, )) != NULL)
{
    entries.name = myfile->d_name;
    stat(buf, &mystat);
    entries.size = mystat.st_size;

    /* store owner's name in struct if it is found using getpwuid_r(). */
    if ((pwd = getpwuid_r(mystat.st_uid, , , , ,)) != NULL)
            entries.user = pwd->pw_name;
    else
         perror("user not found");


    /* store group name in struct if it is found using getgrgid_r(). */
    if ((grp = getgrgid_r(mystat.st_gid, , , , ,)) != NULL)
             entries.group = grp->gr_name;
    else
             perror("group not found");


 }

 int cmpfunc( const void *a, const void *b)
 {
     char const *aa = (char const *)a;
     char const *bb = (char const *)b;

     return strcmp(aa, bb);
 }

 qsort(entries, 4, sizeof(int), cmpfunc);

 int i = 0;
 for(int i; i < sizeOf(entries); i++)
 {
        printf("%s %s %llu %s\n", entries.user, entries.group,
        entries.size, entries.name);
 }

        closedir(mydir);

        return 0;
}

最佳答案

这是使用 readdir_r() 的示例...您应该能够弄清楚其他内容 基于这个例子:

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>

int main(int argc, char *argv[])
{

    DIR *mydir;
    struct dirent myfile;
    struct dirent *result;

    int rc;

    if (argc != 2) {
        perror("must supply a directory");
        return -1;
    }

    mydir = opendir(argv[1]);
    if (mydir == NULL) {
        perror("Cannot find directory");
        return -1;
    }

    while ((rc = readdir_r(mydir, &myfile, &result)) == 0 && result != NULL ) {
        printf("myfile.entryName: -->%s<--  result->d_name: -->%s<--\n",
              myfile.d_name,
              result->d_name);
    }

    closedir(mydir);

}

关于c - 使用 readdir_r 读取目录中的文件并使用 qsort 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27368340/

相关文章:

c++ - Qt 4.8 : trying to sort QList<QStringList> on 1st element of QStringList as integer

c - qsort 指针指向 void

c - xmlsec 库 - 获取签名 key 的主题

c++ - 为什么此代码不返回空矩阵?

c - 如何正确比较 C 中的字符串?

ios - 如何从数组中的所有结构调用参数

c - qsort 函数使用带有 char 指针成员的结构

字符数组打印垃圾,除非我增加数组大小

json - 如何在Go中将具有嵌入式struct字段的结构编码(marshal)为平面JSON对象?

c - 段错误 : 11 when returning stuct via function pointer in struct