Click here to Skip to main content
15,882,802 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi i tried like this

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


But i am getting segmentation fault.Can anyone help me with this?
Posted
Updated 11-Apr-14 4:16am
v2

First, remove the space from your file name, and check the file is in the same folder as the executable - or it won't work.
Then, check your return value from fopen - because if it doesn't find the file, fopen returns null and your code fails, as you have seen.

Then, look at your code! What do you supposed this does:
C++
else{continue;}
That is in any way shape or form useful?
 
Share this answer
 
A paragraph contains two subsequent '\n's, use a variable for counting the two '\n's, like this,

C++
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;
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 12-Apr-14 4:34am    
That does not distinguish between to newlines together and two sentences.
For starters, I assume that you consider a new line to be a new paragraph. i.e.

This is line 1.
This is line 2.

has 2 paragraphs.

What your code does is neglect the case where there is an EOF and not a newline character (\n) after This is line 2.

One way to fix this is to use an extra char variable.

C++
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;
}
 
Share this answer
 
C#
#include <stdio.h>

#define MAX_LEN 100

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

    int pcount = 0;
    int temp = 0;
    while(fgets(line, sizeof line, fp) != NULL) {
        if(line[0] == '\n') {
            if(temp == 1)
                pcount++;
            temp = 0;
            continue;
        }
        else {
            temp = 1;
        }
    }
    printf("number of para in the file is %d\n", pcount);
    return 0;
}

C++

 
Share this answer
 
Comments
CHill60 12-Apr-14 6:04am    
Don't post multiple solutions to the same question - which one is supposed to be correct?
baliram bhande 12-Apr-14 6:18am    
k sir
baliram bhande 14-Apr-14 3:15am    
why u give me down vote .if i post multiple ans is it crime???????????????????????????
i ask those people who can give me down votated
baliram bhande 15-Apr-14 7:02am    
if you agree my answer plz accept it...................

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900