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'

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

Edited 6 Years Ago by c0d3x: n/a

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!

Edited 6 Years Ago by Grn Xtrm: n/a

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

Edited 6 Years Ago by c0d3x: n/a

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

Edited 6 Years Ago by gerard4143: n/a

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?)

Comments
Thanks for corrections

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.

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' )
Comments
brackets among other things... :) Thanks
This question has already been answered. Start a new discussion instead.