[英]C program that reads all characters from an input file
我應該編寫一個 C 程序,該程序從名為 a.txt 的輸入文件中讀取所有字符,並計算並打印每個字符在文件中出現的次數。 忽略字符空格和字符制表符。
我沒有收到任何錯誤,但完全沒有 output。我 go 哪里錯了?
例如下面的文本文件;
對於 Q2:我無法格式化。 不知道這些問題會不會影響。 等級
The output is:
There are 1 of character: 2
There are 1 of character: :
There are 1 of character: F
There are 2 of character: I
There are 1 of character: Q
There are 1 of character: T
There are 1 of character: W
There are 4 of character: a
There are 1 of character: b
There are 2 of character: c
There are 2 of character: d
There are 6 of character: e
There are 4 of character: f
There are 1 of character: g
There are 2 of character: h
There are 2 of character: i
There are 1 of character: k
There are 3 of character: l
There are 2 of character: m
There are 3 of character: n
There are 5 of character: o
There are 1 of character: p
There are 4 of character: r
There are 2 of character: s
There are 5 of character: t
There are 1 of character: w
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
fp = fopen("a.txt", "r+"); //a.txt
int array[127] = { 0 };
char filechar;
while (fscanf(fp,"%d",&filechar))
{
array[(int)(filechar)]++;// Increment
}
for ( int i = 33; i <= 64; i++)
{
switch (i) {
case 33:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '!' );
break;
case 34:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '"' );
break;
case 35:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '#' );
break;
case 36:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '$' );
break;
case 37:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '%' );
break;
case 38:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '&' );
break;
case 39:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '!' );
break;
case 40:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , ')' );
break;
case 41:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '()' );
break;
case 42:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '*' );
break;
case 43:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '+' );
break;
case 44:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , ',' );
break;
case 45:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '-' );
break;
case 46:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '.' );
break;
case 47:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '/' );
break;
case 48:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '0' );
break;
case 49:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '1' );
break;
case 50:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '2' );
break;
case 51:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '3' );
break;
case 52:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '4' );
break;
case 53:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '5' );
break;
case 54:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '6' );
break;
case 55:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '7' );
break;
case 56:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '8' );
break;
case 57:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '9' );
break;
case 58:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , ':' );
break;
case 59:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , ';' );
break;
case 60:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '<' );
break;
case 61:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '=' );
break;
case 62:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '>' );
break;
case 63:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '?' );
break;
case 64:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , '@' );
break;
}
}
for (int i = 65; i <= 90; i++)
{
switch (i) {
case 65:
if (array[i] >0)
printf("There are ", array[i] , " of character: " , 'A' );
break;
case 66:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'B' );
break;
case 67:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'C' );
break;
case 68:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'D' );
break;
case 69:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'E' );
break;
case 70:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'F' );
break;
case 71:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'G' );
break;
case 72:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'H' );
break;
case 73:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'I' );
break;
case 74:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'J' );
break;
case 75:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'K' );
break;
case 76:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'L' );
break;
case 77:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'M' );
break;
case 78:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'N' );
break;
case 79:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'O' );
break;
case 80:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'P' );
break;
case 81:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'Q' );
break;
case 82:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'R' );
break;
case 83:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'S' );
break;
case 84:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'T' );
break;
case 85:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'U' );
break;
case 86:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'V' );
break;
case 87:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'W' );
break;
case 88:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'X' );
break;
case 89:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'Y' );
break;
case 90:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'Z' );
break;
}
}
for (int i = 97; i <= 122; i++)
{
switch (i) {
case 97:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'a' );
break;
case 98:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'b' );
break;
case 99:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'c' );
break;
case 100:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'd' );
break;
case 101:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'e' );
break;
case 102:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'f' );
break;
case 103:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'g' );
break;
case 104:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'h' );
break;
case 105:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'i' );
break;
case 106:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'j' );
break;
case 107:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'k' );
break;
case 108:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'l' );
break;
case 109:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'm' );
break;
case 110:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'n' );
break;
case 111:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'o' );
break;
case 112:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'p' );
break;
case 113:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'q' );
break;
case 114:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'r' );
break;
case 115:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 's' );
break;
case 116:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 't' );
break;
case 117:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'u' );
break;
case 118:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'v' );
break;
case 119:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'w' );
break;
case 120:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'x' );
break;
case 121:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'y' );
break;
case 122:
if (array[i] > 0)
printf("There are ", array[i] , " of character: " , 'z' );
break;
}
}
return 0;
}
首先,線
while (fscanf(fp,"%d",&filechar))
是錯誤的,因為
%d
用於讀取int
,但您傳遞了一個指向char
的指針。 這將導致訪問未分配的空間,並可能導致其他數據的破壞。 %c
應該用於將一個字符讀入char
。fscanf()
到達 EOF 時將返回一個負數,當它用作條件時將被視為 true。該行應該是
while (fscanf(fp,"%c",&filechar) == 1)
其次,您在switch
語句中使用printf()
是錯誤的。
printf()
的第一個參數是格式字符串,第二個或之后的 arguments 是根據格式字符串要使用的數據。
而不是這樣的事情
printf("There are ", array[i] , " of character: " , '!' );
你應該這樣寫
printf("There are %d of character: %c" , array[i], '!' );
或者如果你還想打印換行符
printf("There are %d of character: %c\n" , array[i], '!' );
%d
用於打印 integer, %c
用於打印字符。
排除@MikeCAT 指出的scanf()
和printf()
錯誤,您還應該通過將所有 switch 語句行替換為以下代碼來使您的代碼更短且更易讀:
printf("There are %d of character: %c\n", array[i], i);
您已經手動完成了 ascii 轉換,這是不需要的,因為 printf 語句中的%c
格式說明符正是這樣做的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.