所以我一直在看这段代码好几个小时,尽管我努力在各处使用printf调试它,但似乎无法理解发生了什么。以下功能是我的主要问题所在:

void drawFolders() {

printf("Going to call getSubFolder\n");
getSubFolders(".");

printf("DEBUG 1 - Return from getFolderName is: %s\n", getFolderName(3));

printf("DEBUG 2 - Return from getFolderName is: %s\n", getFolderName(5));

int k;

for(k = 0; k < getNumberFolders(); k++) {
    printf("DEBUG INLOOP %d - Return from getFolderName is: %s\n",k, getFolderName(k));


    draw_sprite(k*60 + 30, 50, folder);

    //draw_string(temp[i].name,i*60 + 30, 50);

}


}

基本上,在DEBUG1和DEBUG2中,我的函数getFolderName()似乎可以正常工作,即,返回的字符串没有问题。但是,当我尝试在for循环内调用此函数时,从getFolderName获得的所有内容都是垃圾(第一个除外),即使k为3或5(我在for循环之前进行过测试并能正常工作的情况) 。 k没错,因为我也打印它。 getFolderName对数组不执行任何操作,以下是函数:

char* getFolderName(int index) {
printf("Values inside getFolderName() function : %s\n", currentFolders[index].name);
return currentFolders[index].name ;


}

同样重要的是要显示currentFolders是:

typedef struct Directories {

    char* name;
    int active;

} Directory;

Directory currentFolders[30];


输出值

Dec 20 22:18:48 192 kernel: Vai chamar o getSubFolder
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: .
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: ..
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: usr
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: lab4
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: proj
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: bin
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: boot
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: dev
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: etc
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: home
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: mnt
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: root
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: sbin
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: tmp
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: var
Dec 20 22:18:48 192 kernel: DEBUG 0Values inside getFolderName() function : lab4
Dec 20 22:18:48 192 kernel: DEBUG 1 - Return from getFolderName is: lab4
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : bin
Dec 20 22:18:48 192 kernel: DEBUG 2 - Return from getFolderName is: bin
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : .
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 0 - Return from getFolderName is: .
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 1 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 2 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 3 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 4 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 5 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 6 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 7 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 8 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 9 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 10 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 11 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 12 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 13 - Return from getFolderName is:
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function :
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 14 - Return from getFolderName is:


我认为其他与该问题无关的函数也可以,但是您可能需要看一下,因此这里仍然是:

  int getSubFolders(char* foldername) {
    num_folders = 0;

    struct dirent *de=NULL;
      DIR *d=NULL;

      d=opendir(foldername);
      if(d == NULL) {
        perror("Couldn't open directory");
        return(2);
      }

      int i = 0;
      // Loop while not NULL
      while(de = readdir(d)) {
        num_folders++;
        Directory temp;
        currentFolders[i].name = de->d_name;

        strcpy( currentFolders[i].name, de->d_name);

//      printf("nome %s\n",de->d_name);

        //printf("temp.name %s\n",temp.name);

      //  currentFolders[i] = temp;

        printf("Values inside getSubFolders function: %s\n",currentFolders[i].name);

        i++;
      }

      printCurrentFolders();

      closedir(d);
      return(0);

}

最佳答案

这样修复,结构体就这样声明它

typedef struct Directories {
    char name[256];
    int active;
} Directory;


并删除此行

currentFolders[i].name = de->d_name;


该问题是由closedir(d);清理opendir所使用的资源引起的,struct dirent *是这些资源的一部分。

09-06 22:11