Hi, I need some assistance in how to count the number of lines in a given file. It is a small part of a homework assignment I have and I am having problems figuring it out.

I am to assume that in each line there will be a maximum number of characters (i.e. 50), and that blank lines do not count. Each line may have less than 50 characters.

Thanks.

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

int main()
{
  FILE* fptr;
  fptr = fopen("data.txt", "r");
  if (fptr == 0)
  {
    printf("Error opening file.");
    return(-1);
  }

  int count = 0;
  int status = 0;
  char temp[50];
  while (status != EOF)
  {
    status = fscanf(fptr, "%50c\n", &temp);
    count++;
  }

  printf("%d\n", count);

  fclose(fptr);

  return(0);
}

I'm assuming this is a text file - Why don't you check for the newline character '\n'

I'm assuming this is a text file - Why don't you check for the newline character '\n'

Yes, they are text files. How do you do that?

Try rewriting the while loop like this:

while ( fgets ( temp, sizeof temp, fptr ) != NULL )

Increment the counter inside the while loop as you have correctly done. Then output the result after the loop.
This is how I have done it successfully in the past. I'm sure there are other ways to do it as well. This is just what I have been able to come up with during my time with C. Good luck!

Try rewriting the while loop like this:

while ( fgets ( temp, sizeof temp, fptr ) != NULL )

Increment the counter inside the while loop as you have correctly done. Then output the result after the loop.
This is how I have done it successfully in the past. I'm sure there are other ways to do it as well. This is just what I have been able to come up with during my time with C. Good luck!

Thanks for your help. However, there's still a problem. My code should not count "blank" lines that may be in between two lines of text.

For example:
Johnny has a car

Matt has a better car

The number of lines should be two. However, your code still counts the blank line in between.

Edit: I worked something out. Does this look ok?

int count = 0;
  char check[81];
  char temp[81];
  while (fgets(temp, sizeof(temp), fptr) != NULL)
  {
    fscanf(fptr, "%s", &check);
    if (check == "\n");
    {
      count++;
    }
  }

Try something like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char**argv)
{
	int ch = 0, i = 0;
	FILE *fd;

	if (!(fd = fopen("filename", "r")))
	{
		fputs("could not open file!\n", stderr);
		exit(EXIT_FAILURE);
	}

	while ((ch = fgetc(fd)) != EOF)
	{
		if (ch == '\n')
			++i;
	}

	fprintf(stdout, "the number of lines->%d\n", i);

	fclose(fd);
	exit(EXIT_SUCCESS);
}

I noticed a few problems in your edit. First of all, remove the semicolon from the if statement. That is undoubtedly throughing off your output. The only other change that seems to be necessary is to test if check is NOT EQUAL to "\n". The following code produced the correct output when I ran it.

int count = 0;
  char check[81];
  char temp[81];
  while (fgets(temp, sizeof(temp), fptr) != NULL)
  {
    fscanf(fptr, "%s", &check);
    if (check != "\n")
    {
      count++;
    }
  }

  printf("%d\n", count);

My text file looked like this:
Panic Attack

Dream Theater

Progressive Rock
Great Band!

The output was 4.

[...]

Apologies for that last post of mine.

You could check for an empty line like this:

char line[50];
      while ( fgets(line, sizeof line, file) )
      {
         if ( line[0] != '\n' )
         {
            ++count;
         }
      }

The syntax used here is not right:

fscanf(fptr, "%s", &check);

Since check is an array of char, you shouldn't have the & there.

This line is really messed up:

if ( check != "\n" )

You've got check as an array and "\n" is a string; do you know what this is comparing here? (That the test is always true?)

commented: Thanks for corrections +2

Many apologies to the OP, I missed the brackets for the check array in the if statement. Thanks to Dave for pointing out the mistakes.

Thanks guys, you've helped me a lot.

I missed the brackets for the check array in the if statement.

There is more than just the brackets here, i.e.

// not "\n", but instead ...
if ( check[0] != '\n' )
commented: brackets among other things... :) Thanks +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.