简体   繁体   中英

How do I read files from an string array of file names?

So im writing a program to open a directory, get all the files inside, and then read the contents of each file. currently i successfully got all the file names in a string array. the print files[] loop shows all the file names, but the loop to check frequency does not read the files correctly. how do i successfully read an array of file names and then scan each of their contents?

//Open Directory
        DIR *dr = opendir(path);
        struct dirent *de;
        if(dr == NULL){
                printf("Could not open directory");
                return 0 ;
        }
        const char* files[100];
        int buffer=0;
        //Read Directory Files
        while((de = readdir(dr)) != NULL){
                files[buffer] = de->d_name;
                buffer++;
        }
        for(int x = 0; x <= buffer; x++){
                printf("%s" , files[x]);
        }
        closedir(dr);
        //Check Frequency
        for(int i = 0; i <= buffer; i++){
                int ch;
                FILE *fp;
                fp = fopen(files[i], "r");
                if(fp == NULL)
                        continue;
                ch = fgetc(fp);
                while(ch != EOF){
                        ch = tolower(ch);
                        if(ch>=97 && ch<= 122){
                                alphabetfreq[ch-97]++;
                        }
                        ch = fgetc(fp);
                }
        fclose(fp);

There are multiple things wrong with the program. But the main reason why it is not reading the files is that you are just passing the file names to fopen(), so it is looking for them in current directory and returning null values. Also you are not handling the null results carefully. And the condition in the loop should x < buffer and not x <= buffer.

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

int main()
{
  int alphabetfreq[100], i;
  for(i = 0; i < 100; i++){
    alphabetfreq[i] = 0;
  }
  char path[] =  "/home/path_to_directory/";
  DIR *dr = opendir(path);
       struct dirent *de;
       if(dr == NULL){
               printf("Could not open directory");
               return 0 ;
       }
       const char* files[100];
       int buffer=0;
       //Read Directory Files
       while((de = readdir(dr)) != NULL){
               files[buffer] = de->d_name;
               buffer++;
       }
       for(int x = 0; x < buffer; x++){
               printf("%s" , files[x]);
       }
       closedir(dr);
       printf("\n");
       //Check Frequency
       for(int i = 0; i < buffer; i++){
               int ch;
               FILE *fp;
               char * file = malloc(strlen(path) + strlen(files[i]) + 1);
               strcpy(file, path);
               strcat(file, files[i]);
               fp = fopen(file, "r");
               if(fp == NULL)
              {
                printf("no file %s\n", file);
                continue;
              }
               ch = fgetc(fp);
               while(ch != EOF){
                       ch = tolower(ch);
                       if(ch>=97 && ch<= 122){
                               alphabetfreq[ch-97]++;
                       }
                       ch = fgetc(fp);
               }

       fclose(fp);
     }

     for(i = 0; i < 26; i++)
     {
       printf("%c %d\n", i+97, alphabetfreq[i]);
     }
}

This is working for me.

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