Thread: "simple" login authorization code

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try this
    Code:
    while(fgets(file_string,50,user_db_file) != NULL) {
        char firstword[50];
        if ( sscanf(file_string,"%s",firstword) == 1 ) {
          printf("First word of the record is %s\n", firstword);
        }
    }
    > fscanf(user_db_file,"%d %s", get_ID, get_pswd);
    Don't forget the & when you're reading into integers.

    > if((strcmp(get_ID, userid)) == 0 && (strcmp (get_pswd, password) == 0))
    You don't use strcmp to compare integers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    how would i extract the 'int'? aka userid from the file?

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you already figured out that %d has something to do with it.

    The rest is just reading the manual page for sscanf, and figuring out how to place the %d in the format string, and supplying a suitable parameter.

    It's looking like you missed a class or two (or a few chapters in the book).
    Because if you'd done the "what is your name, what is your age" exercise, then you would have figured out how to use %d and %s.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    yeah, i know, that's why we're doing this exercise... sink or swim!

    thank you deeply. i have something to work.

  5. #20
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    yeah, i know, that's why we're doing this exercise... sink or swim!

    thank you deeply. i have something to work.

  6. #21
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    I haven't been able to make any head way on this. I can enter the userid and password then the program vomits and dies.

    any other suggestions?

    Code:
    void auth_user()
    {
        char password;
        char get_pswd;
        char userid;
        char get_ID;
    
        printf("\n\n\t\t User Id .... : ");
        fflush(stdin);
        scanf(" %s", &userid);
    
        printf("\n\t\t User Password: ");
        fflush(stdin);
        scanf(" %s", &password);
    
        if((user_db_file = fopen(FNAME1, "r")) == NULL)
        {
            printf("\nFile error!\nAborting...\n");
        }
        else
        {
    
        while(fgets(get_pswd,50,user_db_file) != NULL)
            {
            char firstword[50];
            if (sscanf(get_pswd,"%s",firstword) == 1 )
            {
            printf("First word of the record is %s\n", firstword);
            }
            }
        }
    }

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf(" %s", &userid);
    How many chars did you allocate to store this string?

    > scanf(" %s", &password);
    What about this one?

    > while(fgets(get_pswd,50,user_db_file) != NULL)
    Here, you said you have 50 chars available to fgets(), how many do you actually have?

    The answer is 1 in all cases, you need to use some char arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    this project is going to be too hard for me to do, i may have to give up on it. the help so far at this forum has be been ambiguous at best.

    my hope from this forum was more instruction on how these functions work. i'm also looking at other forums to see if there's any other assistance there too.

  9. #24
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Salem said: "...you need to use some char arrays."

    At the top of the screen is a link that says "C Tutorial", which contains another link called "Arrays". You could also search the internet for a plethora of explanations and examples on character arrays and strings.

    That would be a good time to create a new project, and play around with arrays. See if you understand the material. Try different things. Experiment and learn. If you get stuck somewhere, come back with a specific question on the exact point you're having trouble with.

    The only other option would be for someone to type out a complete explanation for you, repeating information that is already abound on the internet (not to mention in countless books) - which would be a waste of our time. (Well, another option would be for someone to hand you a solution, but that won't promote learning, which is what this forum is about.)

    An important aspect of programming is seeking resources and information, and figuring out things through your own research.

    If the level of assistance you got here is indeed "ambiguous at best", then by all means, search for other forums. But I wouldn't get your hopes up too high that you'll get a magical answer that will give you sudden knowledge without the need for study and practice.

    this project is going to be too hard for me to do, i may have to give up on it.
    You will never get anywhere with anything worthwhile by giving up when it gets difficult. That's where the painful fun, and learning experience, truly begin.

    Maybe you should try not to do too much at once. If your understanding of file I/O and strings are fuzzy, then start small. Start with just strings. Study and write code, and practice until you get the hang of it. Just a little bit at a time. When you have that working to your satisfaction, add a little more code for handling file I/O. You must break the main problem down into smaller problems and tackle them one at a time. This link is often shared here, maybe it will be of value to you: A development process

  10. #25
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    ok i haven't given up yet but still need help i've been given the suggestion to "bring in" strings via a struct.

    i think this is how to do it but really seems weird. i know i'm over my head here but that's why i'm here.

    i have a few more days before i have to give up.

    Code:
    struct user_database
        {
        int user_ID;
        char first_name [50];
        char last_name [50];
        char pass_word [50];
        char user_flag;
        };
    
    
    void auth_user()
    {
        struct user_database get_info;
    
        char password [50];
        char userID [50];
        char buffer [100];
        int i;
    
        printf("\n\n\t\t User Id .... : ");
        fflush(stdin);
        scanf(" %s", &userID);
    
        printf("\n\t\t User Password: ");
        fflush(stdin);
        scanf(" %s", &password);
    
        if((user_db_file = fopen(FNAME1, "r")) == NULL)
        {
            printf("\nFile error!\nAborting...\n");
        }
        else
        {
    
        while(fgets(buffer,50,user_db_file) != NULL)
            {
            if (sscanf(buffer,"%s", get_info.user_ID, get_info.pass_word, get_info.first_name, get_info.last_name, get_info.user_flag) == 1 )
            {
                printf("%s, %s, %s, %s, %s", get_info.user_ID, get_info.pass_word, get_info.first_name, get_info.last_name, get_info.user_flag);
                if ((strcmp(get_info.user_ID,userID) == 0) && (strcmp(get_info.pass_word,password) == 0))
                printf(" it works");
            }
            }
        }
    }

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't know your exact assignment requirements, but this doesn't look too bad.

    On line 34, you only have one "%s" in your "scanf()", but five arguments.

    Compare this to your print statement on line 36, which has five "%s" and five arguments.

    Also, "user_ID" is an integer in your struct, but a character array in your function. You need to determine the data type you want to use to store this value and use it consistently.

    Could you provide a few sample lines of your data file so we can see what you're supposed to be reading? After that, we can start focusing on how to best work your logic to meet the requirements.

  12. #27
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    user.pwd file:

    17556, yellow, S, happy, feet
    402636, black, U, hello, world
    22140, blue, U, dustin, trans
    171820, purple, S, justin, moore

  13. #28
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Sorry for the late reply, I got caught up in stuff last night after work.

    I think the best thing you can do at this point is to (1) determine the logical steps you need to accomplish your task, and (2) tackle each step by itself, one at a time.

    For instance, based on your other posts in this thread, you're not very experienced with the "scanf" family of functions. So rather than assuming how they might work and building code around that, you should thoroughly examine their behavior with small sample programs.

    For instance, the following code replicates the "extract individual strings from a line" approach you are trying to use.

    Code:
    #include <stdio.h>
    
    #define MAX_LEN 8
    
    int main(void)
    {
        char string[] = "test1, test2, test3, test4, test5\n";
        char test[MAX_LEN] = {0};
    
        sscanf(string,"%s",test);
    
        printf("%s\n",test);
    
        return 0;
    }
    
    /*
        output:
    
        test1,
    */
    See that? It reads the first string including the comma. That makes sense, as "scanf("%s",...)" reads characters until it hits the first whitespace (space, tab, newline). So this is something you have to address before incorporating the code into your actual program.

    One option would be to use the additional features of "scanf" to eliminate reading this comma.

    Code:
    #include <stdio.h>
    
    #define MAX_LEN 8
    
    int main(void)
    {
        char string[] = "test1, test2, test3, test4, test5\n";
        char test1[MAX_LEN] = {0};
        char test2[MAX_LEN] = {0};
        char test3[MAX_LEN] = {0};
        char test4[MAX_LEN] = {0};
        char test5[MAX_LEN] = {0};
    
        sscanf(string,"%[^,], %[^,], %[^,], %[^,], %[^,]",test1,test2,test3,test4,test5);
    
        printf("%s\n",test1);
        printf("%s\n",test2);
        printf("%s\n",test3);
        printf("%s\n",test4);
        printf("%s\n",test5);
    
        return 0;
    }
    (See about the "negated scanset" for scanf.) However, if you're unclear on the basics, this would probably not be the best approach for you.

    Another option is "strtok", but again, this might be beyond your abilities at this time.

    In your first post on this thread, you mentioned "fgetc". It's possible to write your own function that takes the entire line and parses it yourself, character by character. This might be the easiest approach for now.

    Hopefully, you should have covered this topic in class. If so, you can use the methods you learned to complete this portion of code.



    So, the point of all this is that you should take everything one step at a time. Break the problem down into smaller problems, and solve each one on its own. I would advise the following:

    1. Write a simple program similar to the first one in this post. Just have a local array with a string that contains one line of your data file. Write the code you need to break that string into separate sub-strings and print them out. Do not advance until this works exactly as expected.

    2. Grow your program to include another array. Read from stdin, parse that string, and print the results. Do not advance until this works exactly as expected.

    3. Add code to your program that will compare the input from stdin with the data parsed from the string in step 1, and print a message indicating if it matches or not. Try different inputs, both correct and incorrect, and see that you get the desired results. Do not advance until this works exactly as expected.

    4. Change your code so that instead of hard-coding the data string, you open the file and load the first line (all the parsing work you did should still work as is). Then test it as you did in step 3. (Note there is no need to add the loop yet - just test the very first line for now.) Do not advance until this works exactly as expected.

    5. Now you can add the loop. After reading input from stdin, enter your loop which reads one line at a time from the data file and compares it to the input. If it's a match, print so and break out of the loop. If you reach the end of the file without a match, print so.



    See how much more manageable this process becomes when you take it in little steps? It might seem like more work at first, but it greatly improves the chances that your program will work as expected as you get closer to finishing it. And this is a lot less time consuming than banging out a bunch of code and staring at it in confusion when it doesn't work.

  14. #29
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    thank you for all your help thus far, but really i'm out of time on this
    project, but really it is what it is... nothing more..

    i may revisit this at a later time and take time to do the baby steps.
    i may come back during my break.

    our hope was to get this project to beta two days ago,
    we'll just have to turn in what we have.

    again thanx!

  15. #30
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    I was fortunate to get an extension on this part, so i don't have to the time to invest or investigate into why this is not working.

    My logic states it should work but I cant see why it's not. PLEASE, help in finishing it up or direct the lines that make it not work.

    additionally, could you stupify your answer, granted i've been trying on this as long as possible.

    PLEASE PLEASE PLEASE! I'm desperate here.

    Code:
        while(fgets(buffer,1000,user_db_file) != NULL)
            {
            if (sscanf(buffer,"%[^,], %[^,], %[^,], %[^,], %[^,]", get_info.user_ID, get_info.pass_word, get_info.first_name, get_info.last_name, get_info.user_flag) == 1 )
            {
                printf("%d %s %c", get_info.user_ID, get_info.pass_word, get_info.user_flag);
                if ((strcmp(get_info.user_ID,userID) == 0) && (strcmp(get_info.pass_word,password) == 0))
                printf(" it works");
                find_result++;
            }
            line++;
            }
            printf("%d Matches found",find_result);
    
                if(find_result == 0)
                    printf("\nSorry, couldn't find a match.\n");
        }
        fclose(user_file);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  2. An interesting code about "struct" in "union"
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2008, 04:37 AM
  3. login "scripts"
    By Thantos in forum Linux Programming
    Replies: 4
    Last Post: 12-02-2003, 08:14 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM