1
Good morning,this function counts the number of lines of a file,but it has a little problem,it only counts the line when it finds the n,for example to test it,I would create a text file,and write a word to see if it was ok,And then I went to find out that it didn’t count because I hadn’t entered at the end of the word and so she couldn’t find the number,can someone else tell me how to count the number without having to worry about giving a word a enter? Function code:
char numerolinhas(){
char contar;
int linhas=0;
FILE *file;
file=fopen("Frutas forca.txt","r");
char conta;
while((conta=fgetc(file))!=EOF){
if(conta=='\n'){
linhas++;
}
}
fclose(file);
return linhas;
Thank you, but could you write the code the way you explained it? I can’t apply it to the code.With the code I can actually learn it.
– Lolzeiro
So it’s easy. With the definition of lines you want to use, just add
1
the number of lines of the other definition...return linhas + 1;
– pmg
I believe that this sum would only apply if the last character is not a ' n'. When arriving at the EOF, this is after the end of the loop, test whether or not the last character read is a ' n' and add 1 if it is not.
– anonimo
Ah!... and as @anonimo says, there is still a third (conditional) possible definition for line :)
– pmg
Thanks for the suggestions,.
– Lolzeiro