#include <stdio.h>
int main (void) {
FILE *fp;
fp = fopen("test.txt", "r");
int char_counter, i, c;
int word_length[12];
char_counter = 0;
for (i = 0; i <= 12; i++) {
word_length[i] = 0;
}
while ((c = getc(fp)) != EOF) {
if (c == '\n' || c == '\t' || c == ' ')
{
word_length[char_counter] = word_length[char_counter] + 1;
char_counter = 0;
}
else {
++char_counter;
}
}
for (i = 0; i <= 12; i++) {
printf("%d %d\n", i, word_length[i]);
}
return 0;
}
的test.txt:直方圖字的長度的調試
blahblahblah blahblah嗒嗒BLA BL BB
輸出:
0 0
1 1
2 1
3 1
4 1
5 0
6 0
7 0
8 1
9 0
10 0
11 0
12 -1 <-- ??
預期輸出看起來相同,但有應該是第12行的1而不是-1。我真的不明白我是如何得到一個負數。
你在做KandR練習1-13/1-14嗎? – Sam
哈哈哈,是的,我是! – lche
我已經發布練習1-13。如果你想要,我也會發布練習1-14 – Sam