【发布时间】:2020-10-22 07:51:39
【问题描述】:
所以我试图创建一个接收输入目录的函数,只检查它的“.txt”文件,然后将所有内容存储到一个字符数组中(这里是动态分配的)。当我对每个文件中的每个字符使用 getc() 时,一次一个,我不仅一次存储每个字符,而且我希望它们一次打印一个字符,以查看是否所有文件正在正确读取。请注意,这里 else 循环中的所有内容在我制作的另一个程序中仅读取单个输入文件时 100% 正确工作。
这是alphabetcount.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"
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() 原型存在于两个 .c 文件的 count.h #include 文件中)
#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
}
这个的输出是......
.
.
..
..
...如果“ printf("%s\n", in_file->d_name ); ”被放置在“If”循环内,但如果它被放置在“else”循环内,我的输出是...
test2.txt
Segmentation Fault
关于我做错了什么有什么建议吗?我认为这与 fopen() 使用不正确有关吗?感谢和抱歉读了这么久!
【问题讨论】:
-
头文件
unistd.h和dirent.h不是 ISO C 的一部分。这意味着您正在使用特定于平台的扩展。这没有什么问题,但是当你这样做时,还请通过设置适当的标签在你的问题中指定平台。据我所知,在这种情况下,标志“posix”可能是合适的。 -
您绝对应该不 fclose(entry_file) 在打开它的else 子句之外。 (即,将 fclose 移到 else 中)。您可以通过限制变量的范围来捕获此类错误。 (即,将其声明移到使用它的块中)
-
检查你的代码,
fclose( entry_file );应该在 if 中 -
谢谢大家!不幸的是,它仍然无法工作:(程序运行但在将 fclose(entry_file); 放置在 else 循环内部 [并且就在 if (entry_file != NULL ) 之外没有打印任何内容
-
将
printf( "Unable to read directory!" );替换为perror(path);错误消息应该提供信息并写入stderr。
标签: c fopen readdir opendir getc