-1
我正在尝试编写一个程序,该程序需要输入一个文本文件并查找最频繁的字符以及多少次。结果我一直得到0。这里是我的代码:C从文本文件中查找频繁字符
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
char *filename;
char ch;
int array[255] = {0};
char str[]= "";
int i, max, index;
// Check if a filename has been specified in the command
if (argc < 2)
{
printf("Missing Filename\n");
return(1);
}
else
{
filename = argv[1];
printf("Filename : %s\n", filename);
}
// Open file in read-only mode
fp = fopen(filename,"r");
// If file opened successfully, then print the contents
if (fp)
{
printf("File contents:\n");
while ((ch = fgetc(fp)) != EOF)
{
for (i = 0; i < 255; i++){
ch = str[i];
}
// Find the letter that was used the most
for(i = 0; str[i] != 0; i++)
{
++array[str[i]];
}
max = array[0];
index = 0;
for(i = 0; str[i] != 0; i++)
{
if(array[str[i]] > max)
{
max = array[str[i]];
index = i;
}
}
printf("The max character is: %c \n", str[index]);
printf("The amount is %d\n", max);
}
}
else
{
printf("Failed to open the file\n");
}
return(0);
}
这是从代码的输出: 文件名:的text.txt 文件内容: 最大特点是:
量为0 最大特点是:
量为0 的最大特点是:
的量为0 的最大特点是:
的量为0
的'for'循环与条件'STR [1] = 0'将被作为'STR运行一次[1]''是0'!。 –
首先'for'循环根本不会运行(根据初始化值)。为什么在那里? – e0k
首先,缩进是正确的。 – BLUEPIXY