简体   繁体   English

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

[英]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.但它不读取文件的主要原因是您只是将文件名传递给 fopen(),因此它正在当前目录中查找它们并返回空值。 Also you are not handling the null results carefully.您也没有仔细处理空结果。 And the condition in the loop should x < buffer and not x <= buffer.并且循环中的条件应该 x < buffer 而不是 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.这对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我从文件中读取字符串并将其存储到数组中,但是我如何知道数组的长度? - I read a string from a file and store it into an array, but how do I know the length of the array? 如何在 C 中读取文件目录以获取所有非文件夹文件的名称 - How do I read a file directory in C to get the Names of all non-folder files 如何从文件中读取字符串数组? - How do i read an array of strings from a file? 我如何从文本文件中读取并使用malloc为文本文件中的每个单词的2d字符串数组分配内存空间 - How do i read from a text file and allocate memory space using malloc for a 2d string array for each word in the text file 如何从文件中读取字符串并将其标记为数组? - How to read string from file and tokenise it into an array? 如何从 txt 文件(仅国家/地区名称)中扫描并将它们存储在二维数组中? - How do I scan from a txt file (only the country names) and store them in a 2D array? 我在打印我的结构时遇到问题(从文件中读取姓名和电话号码)。 我如何解决它? - I am having problems printing my struct (with names and phone numbers read from a file). How do I fix it? 如何将从hdf5文件读取的多个数据存储在一个阵列中? - How do I store multiple data read from hdf5 files in one array? 如何从文件中读取矩阵? - How do I read a matrix from a file? 如何将scanf中的获取名称放入数组中? - How do I put get names from the scanf into an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM