计算文件中的段落

时间:2014-04-12 08:04:41

标签: c file

我在文件系统中工作,我正在计算文件中的段落但是 我没有得到请建议我,我怎么做,我尝试了这个,但没有得到我想要的

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;
    }

5 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

一个段落包含两个后续的'\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;
}

答案 2 :(得分:1)

您的代码计算换行符'\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 file\n");
        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 %d\n", pcount);
    return 0;
}

答案 3 :(得分:0)

对于初学者,我假设您将新行视为新段落。 即。

This is line 1.
This is line 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;
}

答案 4 :(得分: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 %s\n",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 = %d\n",para);
    printf("#lines = %d\n",line);
    printf("#Words = %d\n",word_count);
    printf("#Characters = %d\n",charac);
    printf("Alpha = %d\n",alpha_count);
    printf("Numerical = %d\n",num_count);
    printf("Other = %d\n",non_alnum);
    printf("\n");

    return;
}