【发布时间】:2018-12-29 19:15:38
【问题描述】:
我正在创建一个猜词游戏,它每行读取一个文本文件行,直到找到一个随机单词并将其存储在一个字符串中 (word)。然后用户输入字母,直到显示所存储单词的所有字母。
到目前为止,它运行良好,但每次读取第一个单词时,一些未知字符都会存储在char word[20] 的开头。
请注意,我使用 C 移动应用程序,并且我认为它使用 clang 6.0 编译器。那么错误是来自我的代码还是来自他们的应用程序? (我喜欢)。
这是完整的、更清晰的代码:
//guess the right word
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define NB_OF_WORDS 3 //number of words in text file
main() {
char begin, word[20] = { 0 }, guessedletter;
int num, rightletter, success;
int i = 0;
int show[20] = { 0 };//shown letters
FILE *ressource = NULL;
srand(time(NULL));
if ((ressource = fopen("ressource.txt", "r")) == NULL) {
fprintf(stderr, "Error ressource.txt");//open in read only mode
exit(EXIT_FAILURE);
}
printf("Welcome to Word Guess, a word guessing contest.\n"
"You have to find the letters of a word and guess what word it is.\n"
"Begin? (y/n)\n");
while ((begin = getchar()) == 'y') { //game loop
/*reinitializations*/
fseek(ressource, 3, SEEK_SET);//replaces rewind(ressource)
success = FALSE;
rightletter = 0;
num = 0;
num = rand() % NB_OF_WORDS; //random number between 0 and NB-1
for (i = 0; i < 20; i++) {
show[i] = FALSE;
word[i] = 0;
}
i = 0;
/*end of reinitializations*/
while (i <= num) {//reads line by line until random word is stored
if (fgets(word, 20, ressource) == NULL) {
fprintf(stderr, "fgets did not work");
exit(EXIT_FAILURE);
}
i++;
}
printf("%s", word);//here is just for testing if the read word is well read. Which isn't the case if num=0
for (i = 0; i < 20; i++)
if (word[i] == '\n')
word[i] = '\0';//adds zero character to show where the string ends
while (!success) { //guessing loop
printf("\nWrite a letter: ");
scanf("%c", &guessedletter);
printf("\n");
for (i = 0; word[i] != '\0'; i++) { //compares entered letter to letter from string. If they match...
if (word[i] == guessedletter) {
if (!show[i])
rightletter++;//avoids letters entered twice from increasing count
show[i] = TRUE;
}
if (show[i])
printf("%c", word[i]);//...a letter is revealed...
else
printf("*");//else it stays hidden
}
if (rightletter == strlen(word))
success=TRUE;//if all the right letters found (same number of letters found as number of letters in the words) you win
}
printf("\nCongratulations you have won!\nDo you want to replay? (y/n)");
getchar();//clears newline character
}
fclose(ressource);
return 0;
}
当 num=0 时,我在打印的单词前得到一个奇怪的符号,好像文本文件的第一个字符不应该在那里......
此外,在猜谜游戏中,如果要猜的单词 (word[20]) 是“烦人的”,假设它是“ressource.txt”中的第一个单词 (num=0)。一旦我猜到了所有的字母,这个词就会像这样在屏幕上打印出来:***烦人。列表中的任何其他单词都不会发生这种情况。
我是这个网站的新手,我是用手机发帖的……如有任何错误,我深表歉意。
编辑:删除 fgetc 的 fgets。如果 fgets 读取第一行,仍然会得到几个未知字符。
编辑 2:添加整个代码,将 mot[20] 翻译成 word[20],添加错误测试
编辑 3:替换 rewind(ressource);通过 fseek(资源,3,SEEK_SET);解决了这个问题。这意味着文本文件的开头确实有三个未知字符
【问题讨论】:
-
不要混用你的输入法。上面偷看的
while(begin=='y')是什么? -
循环前调用了scanf:scanf("%c", &begin);如果你输入yes,循环开始,如果你继续输入'y'(yes)会重新开始
-
请参阅scanf() leaves the newline char in the buffer。那是等待
fgets收集,以及您的scanf不过滤空格(如链接所示)。 -
不要混用你的输入法。如果您在主循环中使用
fgets,请在任何地方使用它。您关于“向前移动光标”的说法是无稽之谈。输入流没有“光标”。 -
该文件必须以 Unicode BOM 开头。我敢打赌这些(未提及的)字符是小写的并且带有一些口音,对吧?检查您使用的文本编辑器是否可以在没有 BOM 的情况下保存文件。