C language count the number of lines in the file/*
Example:get file line count in C language
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[]) {
FILE *fileptr;
int count_lines = 0;
char filename[40] = {'t','e','s','t','.','t','x','t'};
char chr;
fileptr = fopen(filename, "r");
chr = getc(fileptr);
if(chr != EOF && chr != '\n')
count_lines = 1;
while (chr != EOF)
{
// Check if it is a line break
if (chr == '\n')
{
count_lines = count_lines + 1;
}
// Gets the next character
chr = getc(fileptr);
}
fclose(fileptr);
printf("there are %d lines in the file %s \n",count_lines, filename);
return 0;
}