繁体   English   中英

从输入目录中只读取 .txt 文件,然后将所有内容获取到 C 中的一个数组中

[英]Reading only .txt files from an input directory, then getc all contents into one array in C

因此,我试图制作一个接收输入目录的 function,仅检查其“.txt”文件,然后将所有内容存储到一个字符数组中(此处为动态分配)。 当我对每个文件中的每个字符使用 getc() 时,一次一个,我不仅一次存储每个字符,而且我希望它们一次打印一个字符,以查看是否所有文件正在正确读取。 请注意,这里的 else 循环中的所有内容在我制作的另一个程序中仅读取单个输入文件时 100% 正确工作。

这是alphabetcount.c,也就是function...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include<unistd.h> 
#include <stdbool.h>
#include <dirent.h>
#include "count.h"

void alphabetlettercount( char *path, char *filetowrite, long alphabetfreq[] )
{
    DIR *dir;
    FILE *entry_file;
    struct dirent *in_file;

    int c, i;
    int filled_elements = 0;

    char *temp_arrayPointer;
    char *perm_arrayPointer;

    perm_arrayPointer = ( char* ) calloc ( 1, sizeof( char ) );

    dir = opendir( path );

    if( dir == NULL )
    {
        printf( "Unable to read directory!" );
        exit( 1 );
    }

    while( ( in_file = readdir( dir ) ) != NULL )
    {

        if ( !strcmp ( in_file->d_name, "." ) || !strcmp ( in_file->d_name, ".." ) || strstr( ( in_file->d_name ), ".txt" ) )
        {

        }


       else
       {

            printf( "%s\n", in_file->d_name );

            entry_file = fopen( in_file->d_name, "r" );

            if ( entry_file != NULL )
            {

                while ( ( c = getc( entry_file ) ) != EOF )
                {
                        *( perm_arrayPointer + filled_elements ) = c;

                        printf( "%c", ( *( perm_arrayPointer + filled_elements ) ) );

                        filled_elements++;        

                        temp_arrayPointer = ( char* ) realloc ( perm_arrayPointer, ( ( filled_elements + 1 ) * sizeof( char ) ) );

                        if ( temp_arrayPointer != NULL )
                        {
                            perm_arrayPointer = temp_arrayPointer;
                        }
            }

        }

        fclose( entry_file );    
    }

    closedir( dir );    
}

这是 testingalphabetcount.c,或者只是 main()...

(注意:alphabetlettercount() 原型存在于 count.h #include 文件中的两个文件中。c 文件)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include<unistd.h> 
#include <stdbool.h>
#include <dirent.h>
#include "count.h"

int main()
{

      char *path = "../data";           // the data *.txt files are under this folder
      
      alphabetlettercount(path);  // process the data files

}

output 到这个是...

.
.
..
..

...如果“ printf("%s\n", in_file->d_name ); ”被放置在“If”循环内,但如果它被放置在“else”循环内,我的 output 是......

test2.txt 
Segmentation Fault

关于我做错了什么的任何建议? 我认为这与 fopen() 使用不正确有关吗? 感谢和抱歉长时间阅读!

我已更改您的 function alphabetlettercount计数以匹配提供的main function

我已经通过使用continue简化了你的else这避免了太大的深度并便于阅读。

我已经移动了专用 scope 中的变量。 我试图保持你的风格,但有些线条很长恕我直言。

您不应该强制转换 calloc(或 malloc)返回。

您的代码的主要问题是fclose的 position 和您仅获取.txt文件的测试(我刚刚添加了缺少的! ,请注意 foo.txt.bar 将被读取)

做这么多的 realloc 不是一个好习惯,尝试分配一个 1k 的缓冲区,每次需要时加倍这个大小会更好

static void alphabetlettercount(const char *path)
{
    DIR *dir;
    struct dirent *in_file;

    int filled_elements = 0;

    char *perm_arrayPointer;

    perm_arrayPointer = calloc ( 1, sizeof( char ) );

    dir = opendir( path );

    if( dir == NULL )
    {
        printf( "Unable to read directory!" );
        exit( 1 );
    }

    while( ( in_file = readdir( dir ) ) != NULL )
    {
        FILE *entry_file;
        int c;
        char filepath[256];
        if ( !strcmp ( in_file->d_name, "." ) || !strcmp ( in_file->d_name, ".." ) || !strstr( ( in_file->d_name ), ".txt" ) )
        {
            continue;
        }

        printf( "%s\n", in_file->d_name );
        snprintf(filepath, sizeof(filepath), "%s/%s", path, in_file->d_name );
        entry_file = fopen( filepath, "r" );
        if ( entry_file == NULL ) {
            printf ("Error opening file: %s\n",strerror(errno));
            continue;
        }

        while ( ( c = getc( entry_file ) ) != EOF )
        {
            char *temp_arrayPointer;
            *( perm_arrayPointer + filled_elements ) = c;

            printf( "%c", ( *( perm_arrayPointer + filled_elements ) ) );

            filled_elements++;

            temp_arrayPointer = realloc ( perm_arrayPointer, ( ( filled_elements + 1 ) * sizeof( char ) ) );

            if ( temp_arrayPointer != NULL )
            {
                perm_arrayPointer = temp_arrayPointer;
            }
        }
        putchar('\n');
        fclose( entry_file );
    }

    closedir( dir );
    free(perm_arrayPointer);
}

暂无
暂无

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

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