【发布时间】:2019-08-11 07:10:11
【问题描述】:
我只需要计算我的“f”文件中的字符数。当我计算一个字符串中的多个字符时,我得到了它的实数,但是当我在我的 .txt 中按“输入”并创建一个新行时,我丢失了 2 个字符。所以,有 4 个字符串和 15 个字符,我的程序告诉我文件中只有 9 个字符。请帮我算一下这个不幸的字符...
这是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;
}
}
}
}
【问题讨论】:
-
被
chars和words初始化为 0? -
我建议您不要在 SO 帖子中使用淫秽语言。暂时编辑一下。
-
我从没见过你从
f得到什么。 -
请提供minimal reproducible example。永远不要写
while (!feof(f))。总是错的。