***一些编辑:int关键字[16];
FILE *secretMessage;
secretMessage = fopen("C://Users/anyah/Fall2019/ECE_114/GreatGatsby.txt", "r");
char c;
char temp[100];
char temp2;
int i = 0;
int j = 0;
以上是声明变量的方式。我使用了atoi(),因为无法将单词保存到数组中
我需要搜索文本文件并找到字符'_'。下划线后面是一个单词,需要将其放入数组中。然后,我需要以与在文本文件中找到的相反顺序打印单词数组。
我了解如何打开文件,创建数组并搜索字符-我在弄清楚如何将找到的字符保存到字符串然后保存到数组中时遇到麻烦。
while(1)
{
c = fgetc(secretMessage);
if (c == '_')
{
while(1)
{
temp2 = fgetc(secretMessage);
if (temp2 == ' ')
break;
else
{
temp[j] = temp2;
j++;
};
}
int words = atoi(temp);
keywords[i] = words;
i++;
}
if (c==EOF)
break;
}
编译时没有错误或警告-这是我当前的输出:
0 4200137 0 16 0 4200393 -1 -1 0 4200160 0 9900960 0 50 0 1
您可能会执行以下操作:
char strings[100][100]; // array of 100 strings, each of them has no more than 99 meaningful characters
while () // outer loop by words
{
while () // inner loop by characters. Don't forget to check EOF here too!
{
strings[i][j] = temp2;
j++;
}
strings[i][j] = 0; // null-terminated string!
i++;
}
while () // printing loop by words
{
printf("%s", strings[i]);
}