2016-06-22 16 views
-2

私はこのファイルhttps://github.com/HaarigerHarald/omxiv/blob/master/omxiv.cを修正しようとしていますが、これは関数getImageFilesInDirだと思います。アルファベット順にアルファベット順(img05.png、img04.png、img03.png、img02.png、img01.png)の順番でディレクトリにファイルを戻さないように変更する必要があるので、イメージビューアが表示されます私の最初のイメージは最高の番号でファイルします(私の場合はimg05.png)アルファベット順にファイルをソート

私はfor(i=0; i-1; i--)のようなものを試しましたが、役に立たなかったです。 どうすればいいですか?

static int getImageFilesInDir(char ***list, const char* path){ 
    struct dirent **namelist; 
    int imageNum; 
    imageNum = scandir(path, &namelist, imageFilter, alphasort); 
    if (imageNum < 0) 
     return imageNum; 
    else { 
     *list=malloc(sizeof(char*) *imageNum); 
     int i; 
     for(i=0; i<imageNum; i++) { 
      if(strcmp(path, ".") == 0 || strcmp(path, "./") == 0){ 
       (*list)[i]= malloc(strlen(namelist[i]->d_name)+1); 
       strcpy((*list)[i], namelist[i]->d_name); 
      }else{ 
       if(strrchr(path, '/')- path != strlen(path)-1){ 
        (*list)[i]= malloc(strlen(path)+strlen(namelist[i]->d_name)+2); 
        strcpy((*list)[i],path); 
        (*list)[i][strlen(path)]='/'; 
        strcpy((*list)[i]+strlen(path)+1,namelist[i]->d_name); 
       }else{ 
        (*list)[i]= malloc(strlen(path)+strlen(namelist[i]->d_name)+1); 
        strcpy((*list)[i],path); 
        strcpy((*list)[i]+strlen(path),namelist[i]->d_name); 
       } 
      } 
      free(namelist[i]); 
     } 
     free(namelist); 
    } 
    return imageNum; 
} 

(これはCと私の最初のタッチである)

+2

[mcve]または[SSCCE(Short、Self Contained、Correct Example)]を使用して**質問を編集してください(http://sscce.org) – NathanOliver

+0

独自の比較コールバック関数'alphasort'を置き換えます。 'scandir()'のRTFMです。 – alk

+0

また、返された名前リスト配列内の項目の順序を逆順にすることもできます。 – FredK

答えて

3

あなただけscandir()alphasort()が何のマイナスを行い比較コールバックを与える必要があります。 、

int descalphasort(const struct dirent **a, const struct dirent **b) 
{ 
    return - alphasort(a, b); 
} 

それとも@chuxで指摘したようにalphasort()パラメータを反転:あなたは文字通りalphasort()出力を否定することができます

int descalphasort(const struct dirent **a, const struct dirent **b) 
{ 
    return alphasort(b, a); 
} 

、代わりにalphasortのごscandir()呼び出しでdescalphasortを使用しています。

+1

'alphasort(b、a);'はどうですか? – chux

+0

@chux良い点。それも同様に動作します。 – Havenard

+0

友人に感謝、魅力のように動作します! :) – peter

関連する問題