Thread: Iterating through elements in an external .txt file help.

  1. #16
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Code:
    int main(void)
    {
            int i, len, passNum=0;
            char passWord[30]={'\0'};
            char **A;
            char input[30];
            char pass[30];
            FILE *fp=fopen("passwords.txt", "r");
            if(!fp)
            {
                    printf("Error! Password file did not open\n");
                    return 0;
            }
            while((fgets(passWord, 30, fp)) != NULL)
            ++passNum;
            printf("\n Enter Password: ");
            scanf("%s",input);
            strcpy(input,input);
            rewind(fp);
            A = malloc(passNum * sizeof(char*));
            for(i=0;i<passNum;i++)
            {
                    A[i]=malloc(30);
            }
            for(i=0;i<passNum;i++)
            {
                    fgets(A[i], 30, fp);
                    len=strlen(A[i])-1;
                    if(*A[len]=='\n')
                    *A[len]='\0';
                            printf("\n current password %s ",A[i]);
                            printf("\n input %s ",input);
            }
            fclose(fp);
            return 0;
    }
    So here is my current working code without the comparing of the user input and each passwrod retrieved from and external file. It's not perfect but after some great help from you guys its getting better. So I want to add the functionality of comparing the users input per password retrievel to see if it matches and either return 'password found' message if found somewhere in the file and 'Not found' if not.

    I have been experimenting with the different ways in which to obtain user input and have gone with scanf, but I have also been told to use fgets from 'Varts' link: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com. when I use scanf as in the current example it seems to pick up the string fine, but its probably the dealing with that string that I am not understanding in terms of clearing buffers,nulling the end of the string etc. Then theres the matter of comparing the input with the passwords, which every time I try and use strcmp - it either always says match, or creates a segmentation fault. tried using strncmp to be more clinical but still failing miserably.

    SO a great answer to my question would be a)How best to obtain and deal with user input. b) once the input is right, how to compare with the passwords from file.

    Great Thanks

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You will have to essentially read from two sources.

    I would not validate the user string. Whatever they type is fine; if they want in they will type the password correctly. Your main goal then is to consume everything that the user types until you find the newline (\n) signaling the end of the password.

    When you read the file you will have to use fgets() and compare the password that was supposed to be typed with what was actually typed. If you know, for example, that this user's password is the third word in the file, then you can use that.

    I'm also assuming that this doesn't involve a real password, because validating those usually involves comparing hashes, etc.
    Last edited by whiteflags; 07-31-2013 at 02:46 AM.

  3. #18
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Ok - so you think that I shouldn't look anymore into the user input other than handling the \n end line (assuming thats when ENTER is pressed?). Then what about the string comparison part, how best to compare the string i.e strcmp as in character by character or strncmp - which is best?

    Do you know of any good examples of code either here,elsewhere or even adhoc code here as an example?
    (and no they're not real passwords, this is justan excersise in getting strings from external files - purely for my own ejoyment/knowledge growth.)

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The program I posted for you, shows how to remove the newline from the end of a string brought in by fgets(). Take it for a spin and see if it doesn't make love with some neurons.

  5. #20
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    I tried it Adak, and I did love it, but broke down at the point in which I tried to apply the same logic to the input string of the user. Although now I think thats not my issue by the sounds of it, it's definately more to do with the strcmp/strncmp functions i'm trying to use to compare the input and passwords - that is doing the opposite of making love to my neurons!

  6. #21
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Code:
     if(!strncmp(input,A[i],len)
     {
               printf("\n MATCH");
     }
    So I've popped this litte bit in to compare, which does a half decent job of making a match - however lets say the password its beta and I type betaabc, it still matches with passwrod 2 in the list. Now i was under the impression that because i've used strncmp, and i've stipulated an int (len) in the function which denotes how many characters to compare (len being the length of the password, my idea being that if the input is longer than the password then it cant possibly match) then this would handle any input error from the user - how wrong i was!

    So i was doing all sorts of testing to break it: could get output to show like this:
    NO MATCH
    NO MATCH
    MATCH
    NO MATCH
    NO MATCH
    NO MATCH - even when the strings dont match....i really might need this spelling out for me guys...

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    strncmp() will find partial matches like that. You are using the wrong function for the job. strcmp() will compare the entire strings with each other, so "beta" never matches "betaabc".

  8. #23
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Code:
    for(i=0;i<passNum;i++)
            {
                    fgets(A[i], 30, fp);
                    len=strlen(A[i])-1;
                    if(*A[len]=='\n')
                    *A[len]='\0';
                    if(!strncmp(input,A[i],len2))
                    {
                            match = 1;
                    }
            }
            if(match == 1)
            {
                    printf("\n That word exists in the file");
            }
            else
            {
                    printf("\n That word doesn't exit in the file");
            }
    Yeah well thats what i was thinking, but I must have been using it wrong because as ive said it either said it always matched regardles of what I input or never matched att all even if it was character perfect. So the code above is what i've come up with that seems to suite the situation perfectly.
    What I was doing wrong before was using the length of the pasword as the int in the strncmp whereas i've changed that now to the length of the user input (len2) and BINGO! Thanks for all the help guys, appreciate it, i'm sure you'll find me again once i post my next problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 12-02-2007, 03:32 AM
  2. opening an external file
    By kpwd in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2002, 10:43 AM
  3. running a external file
    By krappykoder in forum C++ Programming
    Replies: 5
    Last Post: 06-14-2002, 07:19 PM
  4. getting variables from an external .cfg file
    By Ion Blade in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2002, 07:35 PM
  5. Sum of external file
    By robjules in forum C++ Programming
    Replies: 7
    Last Post: 05-08-2002, 09:35 PM

Tags for this Thread