#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:
何とか何とか何とか何とか何とか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 があるはずです。負の数を取得する方法がよくわかりません。