[英]Compare user input with text file in C
I need to have a user input a word then compare the word with a text file to see if it is correct. 我需要让用户输入一个单词,然后将该单词与文本文件进行比较,看是否正确。 The user has 3 attempts to enter the word before the program terminates.
用户有3次尝试在程序终止之前输入单词。 My issue is reading the word from the file I know it's something simple that I have wrong.
我的问题是从文件中读取单词,我知道这很简单,我错了。 I should also clarify that the error I'm getting is in the compiler I haven't gotten to the point of being able to compare the strings yet!
我还应该澄清一下,我遇到的错误是在编译器中,我还没到能够比较字符串的地步!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
fp = fopen("secret.txt","r");
char guess[10];
const char secret[10];
int i, c;
c = getc(fp);
fgets(secret, sizeof(secret), fp);
for (i=0; i < 3; i++)
{
printf("Please guess the word: \n");
scanf("%s", guess);
while (c !=EOF)
{
if (strcmp(secret,guess)==0)
{
printf("Your guess was correct");
return 0;
}
else
{
printf("Your guess was incorrect. Please try again\n");
}
}
fclose (fp);
}
return 0;
}
Here are some pointers: 这里有一些指针:
c = getc(fp)
consumes the first character of the file, so it never becomes part of the secret
variable. c = getc(fp)
使用文件的第一个字符,因此它永远不会成为secret
变量的一部分。 secret.txt
contains a newline, the newline is read into the secret
variable. secret.txt
包含换行符,则将换行符读取到secret
变量中。 while (c != EOF)
loop seems pointless, since c
isn't modified inside the loop. while (c != EOF)
循环似乎毫无意义,因为在循环内部未修改c
。 Furthermore, the infinite nature of the loop prevents the outer for
loop from functioning correctly. for
循环正常运行。 If I were you, I'd fix the while
loop and would make sure that secret
is read correctly, for example by printing it out or examining it in a debugger. 如果您是我,我将修复
while
循环,并确保正确读取secret
,例如通过将其打印出来或在调试器中进行检查。
What is 什么是
c = getc(fp);
needed for? 需要吗? My "guess" would be that you read the first character of the word into
c
and then secret
misses the first character. 我的猜测是,您将单词的第一个字符读入
c
,然后secret
漏掉了第一个字符。
EDIT: Instead of using getc
for EOF checking, which as said corrupts the read word (and this while loop is rubbish anyway), just check the return value of fgets
: 编辑:而不是使用
getc
进行EOF检查,正如所说的那样,它会损坏读取的单词(并且无论如何,这while循环都是垃圾),只需检查fgets
的返回值即可:
if(fgets(secret, sizeof(secret), fp) == NULL)
//file is empty or other error occurred
and remove this infinite while(c != EOF)
loop. 并删除此无限
while(c != EOF)
循环。
So it should rather look something like: 因此,它看起来应该像这样:
FILE *fp = fopen("secret.txt","r");
char guess[10];
const char secret[10];
int i;
if(fgets(secret, sizeof(secret), fp) == NULL)
{
printf("Error while reading file\n");
return -1;
}
fclose(fp);
for (i=0; i < 3; i++)
{
printf("Please guess the word: \n");
scanf("%s", guess);
if (strcmp(secret,guess) == 0)
{
printf("Your guess was correct");
return 0;
}
else
printf("Your guess was incorrect. Please try again\n");
}
return 0;
Your code is grossly off: you do not alter 'c' inside a loop, making it spin indefinitely. 您的代码严重不正确:您不会在循环内更改'c',从而使其无限期旋转。 It's a good idea to sketch your algorithm on a piece of paper before you start coding.
在开始编码之前,先在纸上画出算法草图是一个好主意。 In your case, pseudocode should look like this:
在您的情况下,伪代码应如下所示:
At this point, converting it to C should be more or less mechanical. 此时,将其转换为C或多或少是机械的。 Good luck!
祝好运!
while (c !=EOF)
{
if (strcmp(secret,guess)==0)
{
printf("Your guess was correct");
return 0;
}
else
{
printf("Your guess was incorrect. Please try again\n");
}
}
looks like an infinite loop to me 对我来说似乎是一个无限循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.