FreakZ
FreakZ

Reputation: 3

How to count blank lines from file in C?

So what I'm trying to do is to count blank lines, which means not only just containing '\n'but space and tab symbols as well. Any help is appreciated! :)

char line[300];
int emptyline = 0;
FILE *fp;
fp = fopen("test.txt", "r");
if(fp == NULL)
{
    perror("Error while opening the file. \n");
    system("pause");
}
else
{
    while (fgets(line, sizeof line, fp)) 
    {
        int i = 0;
        if (line[i] != '\n' && line[i] != '\t' && line[i] != ' ') 
        {
           i++;
        }
        emptyline++;
    }
    printf("\n The number of empty lines is: %d\n", emptyline);
}
fclose(fp);

Upvotes: 0

Views: 5831

Answers (4)

rullof
rullof

Reputation: 7424

Before getting into the line loop increment the emptyLine counter and if an non whitespace character is encountred decrement the emptyLine counter then break the loop.

#include <stdio.h>
#include <string.h>

int getEmptyLines(const char *fileName)
{
    char line[300];
    int emptyLine = 0;
    FILE *fp = fopen("text.txt", "r");
    if (fp == NULL) {
        printf("Error: Could not open specified file!\n");
        return -1;
    }
    else {
        while(fgets(line, 300, fp)) {
            int i = 0;
            int len = strlen(line);
            emptyLine++;
            for (i = 0; i < len; i++) {
                if (line[i] != '\n' && line[i] != '\t' && line[i] != ' ') {
                    emptyLine--;
                    break;
                }
            }
        }
        return emptyLine;
    }
}

int main(void)
{
    const char fileName[] = "text.txt";
    int emptyLines = getEmptyLines(fileName);
    if (emptyLines >= 0) {
        printf("The number of empty lines is %d", emptyLines);
    }
    return 0;
}

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 81916

Let's think of this problem logically, and let's use functions to make it clear what is going on.

First, we want to detect lines that only consist of whitespace. So let's create a function to do that.

bool StringIsOnlyWhitespace(const char * line) {
    int i;
    for (i=0; line[i] != '\0'; ++i)
        if (!isspace(line[i]))
            return false;
    return true;
}

Now that we have a test function, let's build a loop around it.

while (fgets(line, sizeof line, fp)) {
    if (StringIsOnlyWhitespace(line))
        emptyline++;
}

printf("\n The number of empty lines is: %d\n", emptyline);

Note that fgets() will not return a full line (just part of it) on lines that have at least sizeof(line) characters.

Upvotes: 0

Rohan
Rohan

Reputation: 551

You are incrementing emptyline on every iteration, so you should wrap it in an else block.

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67175

You should try and get your code right when posting on SO. You are incrementing both i and emptyline but the use el in your call to printf(). And then I don't know what that is supposed to be in your code where it has }ine. Please, at least make an effort.

For starters, you are incrementing emptyline for every line because it is outside of your if statement.

Second, you need to test the entire line to see if it contains any character that is not a whitespace character. Only if that is true should you increment emptyline.

int IsEmptyLine(char *line)
{
    while (*line)
    {
        if (!isspace(*line++))
            return 0;
    }
    return 1;
}

Upvotes: 1

Related Questions