我正在读取目录中的所有文件。以下是我的代码:
DIR* FD;
struct dirent* in_file;
FILE *common_file;
char *c;
FILE *entry_file;
char buffer[1024];
if (NULL == (FD = opendir("/home/fatima/Downloads/thesis/HMP_Dataset/Climb_stairs"))){fprintf(stderr, "Error : Failed to open directory \n"); return 1; }
while((in_file = readdir(FD)))
{
if(!strcmp (in_file -> d_name, ".") ) continue;
if(!strcmp (in_file -> d_name, "..")) continue;
entry_file = fopen(in_file -> d_name , "r");
if (entry_file == NULL) fprintf ( stderr, “Error %s”, strerror(errno);
while (c=fgets (buffer, 1024, entry_file)!=NULL){}
//printf("%s", c);
}
从文件读取内容时,最里面的 while 循环发生段错误。我没有发布堆栈跟踪,因为只打印了段错误。
最佳答案
它引发了 seg 错误,因为 in_file -> d_name 只有文件名。路径也需要添加到它前面。一旦给出完整的文件名和路径,问题就解决了
关于c - 从文件读取内容时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51007792/