简体   繁体   English

将用户输入与C中的文本文件进行比较

[英]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: 这里有一些指针:

  1. c = getc(fp) consumes the first character of the file, so it never becomes part of the secret variable. c = getc(fp)使用文件的第一个字符,因此它永远不会成为secret变量的一部分。
  2. If secret.txt contains a newline, the newline is read into the secret variable. 如果secret.txt包含换行符,则将换行符读取到secret变量中。
  3. The 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: 在您的情况下,伪代码应如下所示:

  • Open file 打开文件
  • Read the secret 阅读秘密
  • Close file 关闭档案
  • Repeat three times: 重复三遍:
  • --- Display the prompt ---显示提示
  • --- Read user input ---读取用户输入
  • --- If user input matches the secret, congratulate the user and exit. ---如果用户输入的内容与机密匹配,请祝贺用户并退出。
  • Tell the user his guess was incorrect. 告诉用户他的猜测不正确。

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.

相关问题 如何在C中使用strcmp比较用户输入和文本文件? - How to compare user input with text file using strcmp in C? 如何将用户输入字符串与C文件中的字符串进行比较 - How to compare user input string with string in a file in C 将用户输入与文本文件进行比较并在C中循环 - Comparing user input with text file and looping in C 如何使用 user.&amp;pass 将用户输入与文件进行比较。 系统在 C 中临时循环 3 次 - How to compare user input to a file using a user.&pass. system while in a temporary loop for 3 times in C 将字符串转换为大写以与C中的用户输入进行比较 - Converting strings to uppercase to compare with user input in C 如何在 C 中的文本文件中的特定行的末尾添加用户输入 - How to add a user input to the end of a specific line in a text file in C 如何将用户给定的输入(字符串)与Ubuntu中的/ etc / passwd文件进行比较 - how to compare user given input(string) to /etc/passwd file in ubuntu in c 如何在C中读取linux etc / passwd文件并比较用户输入名称以进行身份​​验证 - how to read a linux etc/passwd file and compare the user input name for authentication in C 将用户输入与文件中的文本进行比较 - Comparing user input with text in a file 获取基本文件权限以与C中的输入进行比较 - Get basic file permissions to compare with input in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM