samedi 27 février 2016

What can be used to replace the if statement in this bit of code written in C?

The following is my attempt to scan a file to search and see if an entered username is taken already. The code doesn't work because of the if statement.

     for(x=0;x<100;x++)/*for loop allows the user to keep entering usernames until they come up with an unused username.*/
        {
            FILE *userlist;/*The userlist file holds all the taken usernames*/

            userlist=fopen("userlist.txt","r");
            fseek(userlist, x, SEEK_SET);/* This starts the search at the place x, which is incremented each time.*/
            fscanf(userlist," %s", &username_ext);/*This scans the file contents after x and stores it in the username_ext variable*/

            if(strcmp(username,username_ext)==0)/*If the username entered and the strings after the x are the same, the loop terminates.
            {
                printf("\nThe username is already taken.");
                break;
            }
            fclose(userlist);
        }

Thanks!

Aucun commentaire:

Enregistrer un commentaire