Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,167 questions

40,724 answers

573 users

How to count number of lines in a text file in C

1 Answer

0 votes
#include <stdio.h>

int main(int argc, char **argv)
{ 
	FILE *fp; 
    int count = 0;  
    char ch , filename[30] = "d:\\test.php"; 

    fp = fopen(filename, "r"); 
  
    if (fp == NULL) { 
        printf("Error open file"); 
        return 0; 
    } 
	
	ch = getc(fp);
	while (ch != EOF) {
		if (ch == '\n') {
            count = count + 1; 
		}
		ch = getc(fp);
	}
    fclose(fp); 
    
	printf("%d lines\n ", count);
    
	return 0; 
}   
 

/*
run:
 
2514 lines
 
*/

 





answered Jan 17, 2019 by avibootz

Related questions

1 answer 82 views
1 answer 86 views
1 answer 69 views
1 answer 74 views
1 answer 730 views
...