0

I am trying to read all .txt files which is in the directory named "dataset". All text files has name like 1.txt, 2.txt, 3.txt... And then saving the contents of the files to a structure named FILES.

I used the dirent.h library and readdir( ) function as I saw in some sources. But the file name which program read from directory does not return correctly. Here is my related code:

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

typedef struct FILES{
    char **words;
    int wordCount;
}FILES;

void readFiles();

FILES *files;
int fileCount;

int main(){
    readFiles();

    return 0;
}

void readFiles(){
    FILE *file;
    DIR *directory;
    struct dirent *filesInDirectory;
    int counter;

    fileCount = 1;
    files = (FILES *)malloc(sizeof(FILES));
    directory = opendir("dataset");
    if(directory == NULL){
        printf("Warning: The directory name that is given in the code is not 
valid ..!");
        return;
    }else{
        while((filesInDirectory = readdir(directory)) != NULL){
            printf("%s\n", filesInDirectory->d_name);
            file = fopen(filesInDirectory->d_name, "r+");
            if(file == NULL){
                printf("Warning: The file named %s could not open ..!", 
filesInDirectory->d_name);
                return;
            }
            files[fileCount-1].wordCount = 1;
            files[fileCount-1].words = (char **)malloc(files[fileCount-
1].wordCount * sizeof(char *));
            counter = 0;

            while(!feof(file)){
                files[fileCount-1].words[counter] = (char *)malloc(20 * 
sizeof(char));
                fscanf(file, "%s", files[fileCount-1].words[counter]);
                files[fileCount-1].wordCount++;
                files[fileCount-1].words = (char **)realloc(files[fileCount-
1].words, files[fileCount-1].wordCount * sizeof(char *));
                counter++;
            }
            fileCount++;            
            fclose(file);
        }
    }

}

The file name that I printed in here "printf("%s\n", filesInDirectory->d_name);" is ".". Where am I doing wrong?

JollyRoger
  • 737
  • 1
  • 12
  • 38

1 Answers1

0

THIS IS NOT AN ANSWER BUT TOO BIG FOR A COMMENT

Here are your problems:

1.

 files = (FILES *)malloc(sizeof(FILES));

This is enough size of one FILES. This is will not be enough

  1. After

    while((filesInDirectory = readdir(directory)) != NULL){
    

Put something like

size_t len = strlen(filesInDirectory->d_name);
if (len < 5 || strcmp(filesInDirectory->d_name + len - 4, ".txt") != 0) {
  continue;
}

This will check to ensure files end in .txt

  1. Consider using fstat to ensure that the file is a text file

  2. Remove the casts on malloc

  3. Perhaps use realloc for files

  4. while ... feof is bad - see link above

  5. fscanf(file, "%s", - Buffer overruns is possible. Do something about this. Read the manual page

Ed Heal
  • 59,252
  • 17
  • 87
  • 127