简体   繁体   中英

C: Function isn't updating global array (using strcpy / malloc)

Trying to get the files names from a directory in Windows. Doing this by an function called list_files. The function is not doing anything for some reason (or it looks like it), cause whenever I print the array, it is empty.

I have tested this code before and it was working fine. I kept coding (this is a longer code originally) I noticed that it returned 0 (nothing) whenever I had to use the array later on

void list_files();

char *filesList[0][254];
int i = 0, n = 0, l = 254;
char directory[] = {"C:/Users/test/Downloads/test/"};

int main()
{
    list_files();
}

void list_files()
{
    DIR *d;
    struct dirent *dir;
    d = opendir(directory);
    //Determine the number of files
    while((dir = readdir(d)) != NULL) {
        if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
            n++; // determine, count array size based on files
    }
    rewinddir(d);
    //Put file names into the array
    while((dir = readdir(d)) != NULL) {
        if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
        {
            filesList[i][l] = (char*) malloc(sizeof(char) * 100); // allocate memory
            strcpy(filesList[i][l], dir->d_name); // put file names in to array
            i++; // do +1 to read each single array line
        }
    }
    rewinddir(d);
    printf("%s",n);
    for(i;i<n;i++){
        printf("%s/n", filesList[i][l]);
    }
}

What I expect to see (or get) is an array filled in with filenames within a directory. in my case, the directory contains 3 files. The program isn't made for big files with x00 files. So when I print the array:

Hello.txt, bye.txt, bye.exe

Use char *fileList[254] to declare an array of 254 pointers.
use loops for(n = 0; n < i;n++){

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

void list_files();

char *filesList[254];
int i = 0, n = 0, l = 254;
char directory[] = {"./"};

int main()
{
    list_files();
}

void list_files()
{
    DIR *d;
    struct dirent *dir;
    d = opendir(directory);
    //Put file names into the array
    while((dir = readdir(d)) != NULL) {
        if ( ( i < 254) && ! ( strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0))
        {
            filesList[i] = malloc( strlen ( dir->d_name) + 1); // allocate memory
            strcpy(filesList[i], dir->d_name); // put file names in to array
            i++; // do +1 to read each single array line
        }
    }
    for(n = 0; n < i;n++){
        printf("%s\n", filesList[n]);
    }
    for(n = 0; n < i;n++){
        free ( filesList[n]);
    }
}

In addition to the other problems noted, you never reset i in this code:

while((dir = readdir(d)) != NULL) {
    if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
    {
        filesList[i][l] = (char*) malloc(sizeof(char) * 100); // allocate memory
        strcpy(filesList[i][l], dir->d_name); // put file names in to array
        i++; // do +1 to read each single array line
    }
}
rewinddir(d);
printf("%s",n);
for(i;i<n;i++){  
    printf("%s/n", filesList[i][l]);
}

When you reach

for(i;i<n;i++){  

i is likely equal to n . I say "likely" because your code doesn't account for the possibility that the number of entries in the directory can change between your two readdir() loops.

A quick fix for this is to simply set i to zero at the start of the for loop:

for(i=0;i<n;i++){  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM