读取目录中具有相同扩展名的文件并计算其行数

问题描述 投票:0回答:1

我的代码有这个问题。我一直在尝试打开具有相同扩展名的文件,并读取目录中文件的行数。所以,这就是我所做的:

    #include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int countLines(char name[]);
int main()
{
    struct dirent *de;
    DIR *dr=opendir(".");
    char check[16]=".nkt";
    int i;
    char name[64];
    int count=0;


    if(dr==NULL)
    {
        printf("Didn't open!");
        return 0;
    }
    while((de=readdir(dr))!=NULL)
    {
        if((strstr(de->d_name, check))!=NULL)
        {
            strcpy(name, de->d_name);
            countLines(name);
        }
    }



    closedir(dr);


    return 0;
}
int countLines(char name[])
{
    FILE *fp;
    fp=fopen(name,"r");
    char ch;
    int lines=0;
    while(!feof(fp))
    {
        ch=fgetc(fp);
        if(ch=='\n')
        {
            lines++;
        }

    }
    fclose(fp);

    printf("%d\n", lines);

}

而且我得到的结果总是像:

2
2
2

即使每个文件都有54行。很高兴获得一些帮助。PS。扩展名是.nkt

c eof eol
1个回答
0
投票

您显示的countLines()功能正在进入多个陷阱。

  1. fgetc()故意返回fgetc()而不是int。这样做是为了能够返回文件结束状态,以及所有其他可能的字符值。简单的char无法做到这一点。

  2. 使用char标识文件结尾失败,因为仅在最后一次读取到达文件结尾之后才设置EOF指示器。因此,使用feof()控制的循环通常会重复执行一次。

    对此的详细讨论是feof()

  3. 文本文件的最后一行不一定带有文件结尾指示符,但是您很可能仍希望对该行进行计数。需要应用特殊的逻辑来涵盖这种情况。

一个可能实现了上述所有功能的函数的实现可能看起来像这样:

here

关于问题的注释部分中有关不同的行尾指示器的讨论:

文本文件的行尾指示器在不同平台上以不同方式实现(UNIX:#include <stdio.h> /* Returns the number of lines inside the file named file_name or -1 on error. */ long count_lines(const char * file_name) { long lines = 0; FILE * fp = fopen(file_name, "r"); /* Open file to read in text mode. */ if (NULL == fp) { lines = -1; } else { int previous = EOF; for (int current; (EOF != (current = fgetc(fp)));) { if ('\n' == current) { ++lines; } previous = current; } if (ferror(fp)) /* fgetc() returns EOF as well if an error occurred. This call identifies that case. */ { lines = -1; } else if (EOF != previous && '\n' != previous) { ++lines; /* Last line missed trailing new-line! */ } } return lines; } 与Windows:'\n'与...(\r\n))。

为了对此进行操作,默认情况下,C库函数https://en.wikipedia.org/wiki/Newline以所谓的“文本模式”打开文件。如果以这种方式打开,则C实现会注意将每行的结尾作为单个fopen()字符(即所谓的“换行”字符)返回。请注意(如上文第3节所述),对于最后一行,可能根本没有行尾指示符。

© www.soinside.com 2019 - 2024. All rights reserved.