Maybe something like this
input file
line1
line2
line3
line4
Code:
/*Count number of lines*/
#include <stdio.h>
#include <stdlib.h>
#define BORED "/home/cdalten/cstuff/cstuff.txt"
#define MAXLINE 400
int linecount(FILE *fp) {
char buff[MAXLINE];
int count = 0;
while(fgets(buff,MAXLINE,fp) != NULL) {
count++;
}
return count;
}
int main(int arg, char **argv) {
FILE *fp;
int value = 0;
if ((fp=fopen(BORED,"r")) != NULL) {
value = linecount(fp);
}
if(feof(fp)){
printf("The value is: %d \n", value);
return 0;
}
if(ferror(fp)){
perror(argv[0]);
return -1;
}
fclose(fp);
return 0;
}
$gcc -Wall lc.c -o lc
$./lc
The value is: 4
$