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. 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;
}

Answers


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.

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:

if(fgets(secret, sizeof(secret), fp) == NULL)
    //file is empty or other error occurred

and remove this infinite while(c != EOF) loop.

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;

Here are some pointers:

  1. c = getc(fp) consumes the first character of the file, so it never becomes part of the secret variable.
  2. If secret.txt contains a newline, the newline is read into the secret variable.
  3. The while (c != EOF) loop seems pointless, since c isn't modified inside the loop. Furthermore, the infinite nature of the loop prevents the outer for loop from functioning correctly.

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.


Need Your Help

How to drop a draggable into gridster

jquery drag-and-drop draggable gridster

Does anyone know if it is possible to drop a draggable element into gridster?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.