c-计算文件中的段落数



我在文件系统中工作,因为我在计算文件中的段落,但我没有得到,请建议我该怎么做我试过了,但没有得到我想要的

int main()
    {
            FILE *fp=fopen("200_content.txt ","r");
            int pCount=0;
            char c;
            while ((c=fgetc(fp))!=EOF)
            {
                    if(c=='n'){pCount++;}
                    else{continue;} 
            }
            printf("%d",pCount);
            return 0;
    }

您应该将c声明为int,而不是char

另外,在main()返回之前,请记住fclose(fp);

一个段落包含两个后续的'n',使用一个变量来计算这两个'n',如下所示,

int main()
{
        FILE *fp=fopen("200_content.txt ","r");
        int pCount=0;
        char c;
        int newln_cnt=0;
        while ((c=fgetc(fp))!=EOF)
        { 
                if(c=='n')
                {
                  newln_cnt++;
                  if(newln_cnt==2)
                  {
                     pCount++;
                     newln_cnt=0;
                  }
                }
                else{continue;} 
        }
        printf("%d",pCount);
        return 0;
}

代码计算换行'n'字符的数量,而不是分隔段落的空行。使用fgets从文件中读取行。我建议这个-

#include <stdio.h>
// maximum length a line can have in the file. 
// +1 for the terminating null byte added by fgets
#define MAX_LEN 100+1   
int main(void) {
    char line[MAX_LEN];
    FILE *fp = fopen("200_content.txt", "r");
    if(fp == NULL) {
        printf("error in opening the filen");
        return 1;
    }
    int pcount = 0;
    int temp = 0;
    while(fgets(line, sizeof line, fp) != NULL) {
        if(line[0] == 'n') {
            // if newline is found and temp is 1 then
            // this means end of the paragraph. increase
            // the paragraph counter pcount and set temp to 0
            if(temp == 1)
                pcount++;
            temp = 0;
        }
        else {
            // if a non-empty line is found, this means
            // the start of the paragraph
            temp = 1;
        }
    }
    // if the last para doesn't end with empty line(s)
    if(temp == 1)
        pcount++;
    printf("number of para in the file is %dn", pcount);
    return 0;
}

首先,我假设您将新行视为新段落。即

This is line 1.
This is line 2.

共有2段。

您的代码所做的是忽略This is line 2. 后面有EOF而不是换行符(\n)的情况

解决此问题的一种方法是使用额外的char变量。

int main()
{
        FILE *fp=fopen("200_content.txt ","r");
        int pCount=0;
        char c; // char that checks
        char last_c; //record of the last character read in the loop
        while ((c=fgetc(fp))!=EOF)
        {
                if(c=='n'){pCount++;}
                last_c = c;
                else{continue;} //this line is redundant. You can remove it 
        }
        if (last_c != 'n') pCount++; //if EOF at the end of line and not 'n'
        printf("%d",pCount);
        return 0;
}
void analyze_file(const char *filename) {
    FILE* out_file;
    out_file = fopen(filename,"r");
    int size;
    if(out_file == NULL)
    {
        printf("Error(analyze_file): Could not open file %sn",filename);
        return;
    }
    
    fseek(out_file,0,SEEK_SET);
    char ch,ch1;
    int alpha_count = 0,num_count = 0,non_alnum =0,charac=0;
    int word_count =0,line=0;
    int para=0;
    while(!feof(out_file))
    {
        ch = fgetc(out_file);
        if (isalpha(ch))
            alpha_count++;
        else if(isdigit(ch))
            num_count++;
        else if(!isalnum(ch) && ch!='n'  && !isspace(ch))
            ++non_alnum;
        else if(ch=='n')
        {   line++;
        ch1 = fgetc(out_file);// courser moves ahead , as we read
        fseek(out_file,-1,SEEK_CUR); // bringing courser back
        }
        else if(ch == ch1)
            {para++; //paragraph counter
            word_count--;
            }

        if(ch==' '||ch=='n')
                            {
                                word_count++;
                            }
        if(ch==EOF)
        {
            word_count++;line++;para++;
        }
    }
    non_alnum -=1;// EOF character subtracted.
    charac = alpha_count + non_alnum + num_count;
    fclose(out_file);
    
    printf("#Paragraphs = %dn",para);
    printf("#lines = %dn",line);
    printf("#Words = %dn",word_count);
    printf("#Characters = %dn",charac);
    printf("Alpha = %dn",alpha_count);
    printf("Numerical = %dn",num_count);
    printf("Other = %dn",non_alnum);
    printf("n");
    return;
}

相关内容

  • 没有找到相关文章

最新更新