サイトマップ / C言語講座>出入り口>総目次>目次:ファイル>空白文字の出現頻度||デモ用
ソースプログラムをコンパイルして実行する際には、ファイル名を”WTest.txt”に変更して、同じフォルダに置いてください。
#include <stdio.h>
void main(void);
void main(void)
{
FILE *fp;
unsigned long space, cr, tab, other;
int c;
space = cr = tab = other = 0L;
if ((fp = fopen("BitShift.c", "r")) == NULL) {
fprintf(stderr, "Can't Open C Source File!\n");
exit(2);
}
while ((c = getc(fp)) != EOF) {
switch (c) {
case ' ': space++;
break;
case '\n': cr++;
break;
case '\t':tab++;
break;
default:other++;
}
}
printf("File name: BitShift.c\n");
printf("Space:%lu\tCR:%lu\tTab:%lu\tOther:%lu\n",
space, cr, tab, other);
fclose(fp);
}