我想知道为什么我的程序得到错误的结果。我只需要从文件中读取字符,包括空格、(.)、(,) 和其他字符,然后以特定的字符串格式输出它们。结果,我得到了更多 300 个字符读取实例及其奇怪的表示。我需要打印实际字符及其内存位置。欣赏它!
34 instances of character :
35 instances of character ¼
36 instances of character ¼
37 instances of character ¼
#include <stdio.h>
int main()
{
char ch;
int characters[100];
int i,j,k;
int charCount = 0;
FILE *fPtr;
fPtr=fopen("input.txt", "r");
while(ch = fgetc(fPtr) != EOF)
{
i=0;
characters[i] = (char)ch;
i++;
}
int arrayLength = sizeof(characters)/sizeof(characters[0]);
for(j= 0; j < arrayLength; j++)
{
for(k=1; k < arrayLength; k++)
{
if(characters[j] == characters[k])
{
charCount++;
printf("%d instances of character %c in location %c \n", charCount, characters[j], &characters[j]);
}
}
}
}