Characters, words and lines counting
Hi all, newbie here. I was trying to write a program that counts characters, words and lines from a text file. I wrote a function for every task, and because i'm pretty sure the problem is there i will post only that... if you need the whole program please tell me.
Characters:
Code:
int contaCaratteri(FILE* file){
char buffer;
int caratteri = 0;
while (!feof(file)){
fscanf(file, "%c",&buffer);
if(buffer != ' ' || buffer != '\b' || buffer != '\n'){
caratteri++;
}
}
return caratteri;
}
I don't want to include spaces, newline and EOF in the count...
lines:
Code:
int contaRighe(FILE* file){
int righe = 1;
char buffer;
while (!feof(file)){
fscanf(file, "%c",&buffer);
if(buffer == '\b' || buffer == '\n'){
righe++;
}
}
return righe;
}
I thought that every newline means a line more, and the first is sure :D
words:
Code:
int contaParole(FILE* file){
int parole = 1;
char buffer;
while (!feof(file)){
fscanf(file, "%c",&buffer);
if(buffer == ' ' || buffer == '\b' || buffer == '\n'){
parole++;
}
}
return parole;
}
same here: every space or newline, means a word, and the first is sure...
To speak the truth, i'm really not sure of the if conditions, i think the problem is there...
now, that is the answer of the program.. eg.
bla
bla
bla
13 characters (instead of 9)
1 line (instead of 3)
1 word (instead of 3)
thank you so much for your help!