+ 3

I am trying to count the number of lines of files with this function but things finish with an ifinite loop !!

#include<stdio.h> #include<stdlib.h> long count(char * fileName ) {     long compteur = 0 ;     char caracter ;    fich = fopen( fileName,"r") ;    if ( fich != NULL )    {       do        {                 caracter = fgetc(fich) ;                 if ( caracter = '\n' )                          compteur++ ;       }while ( caractere != EOF ) ;  }  fclose(fich) ;  return compteur ; } ;

19th Jul 2017, 7:08 PM
ayoub soussi
ayoub soussi - avatar
5 Antworten
+ 3
I found it !! 😃 it's a stupid fault , in the condition of "if" I had to use "==" not "=" !! thanks for trying help 😊
20th Jul 2017, 12:35 PM
ayoub soussi
ayoub soussi - avatar
+ 2
the main function is simple : ( "dictionnary.txt" is a file that contain 8 words, each word in a line) int main() { printf(" the number of words is : %ld", count("dictionnary.txt") ); return 0 ; }
20th Jul 2017, 12:02 PM
ayoub soussi
ayoub soussi - avatar
+ 2
I am working on windows, the eol is '\n'
20th Jul 2017, 12:02 PM
ayoub soussi
ayoub soussi - avatar
+ 1
where is your main() function? Also if the file was created in Windows, I think the eol chatacter is '\r'
20th Jul 2017, 2:23 AM
Gurpreet Singh
Gurpreet Singh - avatar
+ 1
I also overlooked it :)
20th Jul 2017, 1:30 PM
Gurpreet Singh
Gurpreet Singh - avatar