I need just to count the number of chars in my 'f' file. When I count a number of chars in one string, I get the real number of it, but when I press 'enter' in my .txt and creating a new line I lose 2 chars. So, having 4 strings with 15 chars at all,my program telling me that there are only 9 chars in the file. Help me please to, just, count this unfortunate chars...
Here is a code in C:
while (!feof(f)) {
bool space = 1; //remembering last char (was it space or not)
for (int i = 0; i < len; ++i) { //running till the end of string
if (a[i] == ' ') {space = 1;}
else if (a[i] != '\0' && a[i] != '\n') {
chars++;
if (space == 1) {
words++; //and counting the words
space = 0;
}
}
}
}
In my opinion, the following would suffice:
int c;
while ((c=fgetc(f))!=EOF) {
if (c == ' ' || c=='\n') {
words++;
}
else {
chars++;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.