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