Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为scandir()实现自定义compar()_C_Linux_Sorting_Alphabetical_Scandir - Fatal编程技术网

如何为scandir()实现自定义compar()

如何为scandir()实现自定义compar(),c,linux,sorting,alphabetical,scandir,C,Linux,Sorting,Alphabetical,Scandir,我一直在读scandir()和alphasort()的手册页,显然已经把它们都塞满了。 但是仍然不知道如何实现自定义比较函数 这是我的密码: #include <stdio.h> #include <dirent.h> #include <sys/types.h> int mySort(char*, char*); int (*fnPtr)(char*, char*); int main(){ struct dirent **entryList;

我一直在读scandir()和alphasort()的手册页,显然已经把它们都塞满了。 但是仍然不知道如何实现自定义比较函数

这是我的密码:

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

int mySort(char*, char*);
int (*fnPtr)(char*, char*);

int main(){ 
    struct dirent **entryList;
    fnPtr = &mySort;
    int count = scandir(".",&entryList,NULL,fnptr);
    for(count--;count>=0;count--){
        printf("%s\n",entryList[count]->d_name);
    }
    return 0;
}

int mySort(const void* a, const void* b){
char *aNew, *bNew;
if(a[0] == '.'){
    *aNew = removeDot(a);
}
else{
    aNew = a;
}

if(b[0] == '.'){
    *bNew = removeDot(b);
}
else{
    bNew = b;
}
return alphasort(aNew, bNew);
#包括
#包括
#包括
int mySort(char*,char*);
int(*fnPtr)(字符*,字符*);
int main(){
结构方向**入口列表;
fnPtr=&mySort;
int count=scandir(“.”,&entryList,NULL,fnptr);
对于(计数--;计数>=0;计数--){
printf(“%s\n”,entryList[count]->d_name);
}
返回0;
}
int mySort(常量无效*a,常量无效*b){
char*全新,*bNew;
如果(a[0]='.'){
*重新=移除(a);
}
否则{
重新=a;
}
如果(b[0]='.')){
*bNew=移除的目标(b);
}
否则{
bNew=b;
}
返回alphasort(新的,bNew);
}

很容易看出,我正在尝试按字母顺序对文件名进行排序,而不考虑隐藏文件和普通文件(以“.”开头)


但是计算机总是按照你的要求去做,而不是按照你的要求去做。

排序例程
mySort
是个问题。此比较函数的类型必须为
int(*)(const-struct-dirent**,const-struct-dirent**)
。例如:

int mySort(const struct dirent **e1, const struct dirent **e2) {
  const char *a = (*e1)->d_name;
  const char *b = (*e2)->d_name;
  return strcmp(a, b);
}

建议改为

int mySort(const struct dirent **e1, const struct dirent **e2);
int (*fnPtr)(const struct dirent **e1, const struct dirent **e2);