Thread: Assigning line of file to string

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    8

    Assigning line of file to string

    I'm making a game that requires the user to enter a correct username and password to play. I have a list of accepted ones saved in a txt file. How do i read each line of the file and check it with the one entered? I know how to use strcmp(string1, string2). I just need to know how to move from one line to the next. This is what I have for this part of the program so far. Also, I think im using the feof() wrong.


    Code:
      id = fopen("F://CS223/namepass.txt", "r");
      score = fopen("F://CS223/scores.txt", "a");
      
      srand(time(NULL));
      
      if(id == NULL)
      {
            printf("ERROR");
      }
      
      else
      {
           printf("Enter your name and password, like this: (name_password)\n\n");
           gets(input);
           while(!feof(id))
           {
                fscanf(id,"%s",passw);
                if(strcmp(input,passw)==0)
                {
                     i=1;
                }   
                else
                {
                     i=0;
                }
           }
           if(i==1)
           printf("\nACCESS GRANTED\n\n");
           else
           {
               printf("\n\nACCESS DENIED\n\n");
               system("PAUSE");
               exit(0);
           }      
           fclose(id);  
      }
    Last edited by Bobby Damore; 03-28-2014 at 07:13 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    read on why feof and gets are bad, depending on the format of your text file , you could use the return value of fscanf() to control your loop,the rest should be straight forward

    ....
    i must add that its pointless to keep on checking the rest of the file when the password has already been found
    Last edited by africanwizz; 03-28-2014 at 07:31 PM. Reason: meh..

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    8
    I changed it to this and it is still not granting me access

    Code:
           while((fscanf(id,"%s",passw))==1)
           {
                if(strcmp(input,passw)==0)
                {
                     i=1;
                }   
                else
                {
                     i=0;
                }
           }

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    8
    It only works when I enter the password on the very last line of the text file.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That's because your code resets i to zero whenever a line is found in the file that does not match user input. So, if there is anything after a match occurs, i will be reset.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    8
    i has nothing to do with reading the file though. I just don't know why only the last line of my file is being read.

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    8
    ahhhh I see the problem with the else statement. got rid of it and it works great now, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read file line, search for string, get string position
    By taifun in forum C++ Programming
    Replies: 15
    Last Post: 03-24-2014, 02:55 PM
  2. Replies: 2
    Last Post: 08-06-2012, 08:59 AM
  3. Read command line and write to file or string
    By miiisuuu in forum C Programming
    Replies: 1
    Last Post: 10-21-2007, 11:39 AM
  4. help with fgets and assigning line to variable
    By pabl_o in forum C Programming
    Replies: 5
    Last Post: 04-11-2005, 09:20 PM
  5. removing a single string/line from a text file
    By turmoil in forum C Programming
    Replies: 5
    Last Post: 04-06-2005, 03:44 PM