c-Qsort未按正确顺序显示



我使用qsort对ProcEntry指针数组进行排序,并试图将它们排序到升序,但由于某些原因,只有几个指针不合适。我已经为defaultSort函数尝试了多种类型的比较器,但似乎不明白为什么有些值没有被排序。

#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <ctype.h>
#include "ProcEntry.h"
#define MAX_PATH_LENGTH 4096
#define UNUSED(x) (void)x
static int defaultFilter(const struct dirent *current){
return isdigit(current->d_name[0]);
}
static int defaultSort(const void * procEntryPtr1, const void * procEntryPtr2){
/* Dereference the parameters to access the ProcEntry pointers */
ProcEntry *ProcEntry1 = *(ProcEntry **)procEntryPtr1; 
ProcEntry *ProcEntry2 = *(ProcEntry **)procEntryPtr2;
// if((ProcEntry1)->pid < (ProcEntry2)->pid) return -1;
// if((ProcEntry1)->pid == (ProcEntry2)->pid) return 0;
// if((ProcEntry1)->pid > (ProcEntry2)->pid) return 1;

return (ProcEntry2->pid > ProcEntry1->pid) - (ProcEntry2->pid < ProcEntry1->pid);
//return ProcEntry1 < ProcEntry2 ? -1 : ProcEntry1 > ProcEntry2 ? 1 : 0;
}
int main (int argc, char * argv[]) {
struct dirent **namelist;
int n;
int opt;
int print = 0; // 0 for no 1 for yes
/* Declare filterFunction pointer */
int(*filterFunction)(const struct dirent *);
filterFunction = defaultFilter;
/* Declare filterFunction pointer */
int(*sortFunction)(const void *, const void *);
sortFunction = defaultSort;
char* dir = "/proc";
/* Variable Fields */
while((opt = getopt(argc, argv, "d:pczh")) != -1){
switch(opt){
case 'd':
dir = argv[2];
if(strcmp(dir, "/proc") == 0){
filterFunction = defaultFilter;
} else {
filterFunction = NULL;
}
print = 1;
break;
case 'p':   //Part 3
print = 1;
filterFunction = defaultFilter;
sortFunction = defaultSort;
break;
case 'c':   //Part 3
break;
case 'z':   //Part 3
break;
case 'h':
print = 0;
printf("Usage: %s [-d <path>] [-p] [-c] [-z] [-h]n", argv[0]);
printf("t-d <path> Directory containing proc entries (default: /proc)n");
printf("t-p        Display proc entries sorted by pid (default)n");
printf("t-c        Display proc entries sorted by command lexicographicallyn");
printf("t-z        Display ONLY proc entries in the zombie state n");
printf("t-h        Display this help messagen");
break;
default:
fprintf(stderr,"Error: Invalid Option Specifiedn");
fprintf(stderr, "Usage: %s [-d <path>] [-p] [-c] [-z] [-h]n", argv[0]);
printf("t-d <path> Directory containing proc entries (default: /proc)n");
printf("t-p        Display proc entries sorted by pid (default)n");
printf("t-c        Display proc entries sorted by command lexicographicallyn");
printf("t-z        Display ONLY proc entries in the zombie state n");
printf("t-h        Display this help messagen");
}
}
if(argc > 6){
fprintf(stderr, "Usage: ./myps [-d <path>] [-p] [-c] [-z] [-h]n");
printf("t-d <path> Directory containing proc entries (default: /proc)n");
printf("t-p        Display proc entries sorted by pid (default)n");
printf("t-c        Display proc entries sorted by command lexicographicallyn");
printf("t-z        Display ONLY proc entries in the zombie state n");
printf("t-h        Display this help messagen");
}
n = scandir(dir, &namelist, filterFunction, alphasort);
qsort(namelist, n, sizeof(ProcEntry *), sortFunction);
if (n < 0)
perror("scandir");
else {
while (n--) {
printf("%sn", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
/* display the names on stdout stream */
if(print == 1){
print = 0;
int i;
for (i = 0; i < n; ++i) {
fprintf(stdout,"%sn", namelist[i]->d_name);
}
}

return 0;
}

您要求qsort()namelist(struct dirent **(进行排序。你说每个元素的sizesizeof(ProcEntry *)而不是sizeof(*namelist),这很奇怪,但我想这没关系,因为指针的大小相同。然而,您的排序函数传递了两个struct dirent **,虽然您没有告诉我们ProcEntry是什么,但类型转换*(ProcEntry **)几乎肯定是不正确的。

这将使用单独的qsort():按名称(d_name(对结果进行排序

#define _POSIX_C_SOURCE 200809L
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int dirent_cmp(const void *a, const void *b) {
return strcmp(
(*(struct dirent **) a)->d_name,
(*(struct dirent **) b)->d_name
);
}
int main (int argc, char * argv[]) {
struct dirent **namelist;
int n = scandir(".", &namelist, NULL, NULL);
qsort(namelist, n, sizeof(*namelist), dirent_cmp);
for(int i = 0; i < n; i++) {
printf("%sn", namelist[i]->d_name);
}
}

如前所述,如果指定一个比较函数作为第四个参数,scandir()将在内部为您对结果进行排序(将比较函数重命名为dirent_cmp2(),以强调声明使用不同类型的ab(:

#define _POSIX_C_SOURCE 200809L
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int dirent_cmp2(const struct dirent **a, const struct dirent **b) {
return strcmp((*a)->d_name, (*b)->d_name);
}
int main (int argc, char * argv[]) {
struct dirent **namelist;
int n = scandir(".", &namelist, NULL, dirent_cmp2);
for(int i = 0; i < n; i++) {
printf("%sn", namelist[i]->d_name);
}
}

相关内容

  • 没有找到相关文章

最新更新