Reputation: 1
Is there a way to count the total number of blank lines in a file in C language. I have been trying and below is my code but it is not working.
void countLines(char *f1)
{
FILE *fp;
fp=fopen(f1,"r");
int d=fgetc(fp),count=0,countbl=0;
while(d != EOF)
{
if(d=='\n')
{
d=fgetc(fp);
if(d=='\n')
countbl++;
count++;
fseek(fp,-1,SEEK_CUR);
}
d=fgetc(fp);
}
printf("%d %d",count+1,countbl);
}
I am using fgetc to read the file charcater by charcater when fgetc encounters \n
I check the next character if it is again \n
that means it is blank line but it is not working.
Sample Input
fileCount.txt(name of text file)
Contents of File
This is line one
Hello, welcome to programming
Code quotient - Get better at coding
b
Sample Output
8 4
Here 8 is the total number of lines and 4 is the total number of blank lines.
Here is the new code but still it is not working
void countLines(char *f1)
{
FILE *fp;
fp=fopen(f1,"r");
int d=fgetc(fp),count=0,countbl=0,temp=0;
while(d != EOF)
{
//printf("%c %c",temp ,d);
if(d=='\n' && temp=='\n' )
countbl++;
if(d=='\n')
count++;
temp=d;
d=fgetc(fp);
}
printf("%d %d",count+1,countbl);
}
Upvotes: 0
Views: 789
Reputation: 1
So I came with this new code, instead of counting blank lines what I did is count the total number of lines and the total number of filled lines and then subtracted filled lines from total lines and it works perfectly I don't know why earlier code was not working which was suggested by @MOehm may be due to compiler issue or I don't know.
Here is the code
void countLines(char *f1)
{
FILE *fp;
fp=fopen(f1,"r");
int count=0,countbl=0;
char str[200],*c1;
c1=fgets(str,200,fp);
while(c1 != NULL)
{
if(c1[0]>=32 && c1[0]<=127)
countbl++;
c1=fgets(str,200,fp);
count++;
}
printf(" %d %d",count,count-countbl);
}
Upvotes: 0
Reputation: 29116
First, let's define a structure to hold the line statistics, so that we can return t from the function:
struct linestat {
size_t total;
size_t blank;
};
Next, let's define what consitutes a blank line. Your definition of two consecutive newline characters it okay, but rather narrow, so let's say that a blank line consists of only space characters and it is terminated by a newline character. You can determine whether a character c
is whitespace or not with the function isspace(c)
from <ctype.h>
.
You can go about this in different ways. Here's one way:
Scan a line until a newline character is seen and keep track of whether you encounter non-spaces on your way. Recoed that line in your stats. repeat. You must also check whether you encounter EOF
's, of course:
struct linestat count_lines(const char *filename)
{
struct linestat ls = {0, 0};
FILE *fp = fopen(filename, "r");
if (fp == 0) return ls;
while (1) {
int blank = 1;
int c = fgetc(fp);
while (c != EOF && c != '\n') {
if (isspace(c) == 0) blank = 0;
c = fgetc(fp);
}
if (c == EOF) break;
ls.total++;
if (blank) ls.blank++;
}
fclose(fp);
return ls;
}
Notes:
ungetc
is better than fseek
ing.)while (1)
with a break
here, because I think it matches how the procedure works better. Note that the final line of a file will not be considered if it isn't properly terminated with a newline character.fopen
ed a file, you must make sure to fclose
it later.Upvotes: 1