如何从文件名的字符串数组中读取文件?

问题描述:

因此,我正在编写程序以打开目录,将所有文件放入其中,然后读取每个文件的内容.目前,我已成功获取字符串数组中的所有文件名. print files []循环显示所有文件名,但是用于检查频率的循环无法正确读取文件.我如何成功读取文件名数组,然后扫描它们的每个内容?

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);

程序有很多问题.但是不读取文件的主要原因是您只是将文件名传递给fopen(),因此它正在当前目录中查找它们并返回空值.另外,您没有仔细处理空结果.并且循环中的条件应x

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]);
     }
}

这对我有用.