-3

Ciao,

Look at my code below. That`s a standard program for Log in authentication. In the file "Login.txt" located 1 string with user name. When program starts, it asks to enter the user name, then compare this string with the string saved in "Login.txt", if both equal then - success, if not equal, then we can try only 3 times till the program ends; I need to save in "Login.txt" 3 strings (user names) for example: Brad David Peter

So I need to call each string from "Login.txt separately to char buf[], then to call scanf function (David - for example), after use strcmp for both, and as a result both strings should be equal each other;

Question: How to extract multiple strings "separately" and put each to different char buf[] from a single file in C FILE .txt?

Thank you in advance.

CODE:

int main() {

    system("cls");
    FILE *p;
    int n=3,b,v;
    char buf[20],buf2[20],Login[20],Password[20];
    p=fopen("Login.txt","rt");
    fgets(buf,20,p);
    fclose(p);
    p=fopen("Password.txt","rt");
    fgets(buf2,20,p);
    fclose(p);
    printf("%s",buf);         
    do { 
        printf("Please Log in. You have %d tries\n",n);
        printf("Enter your Login\n");
        scanf("%s",Login);
        printf("\nEnter your Password\n");
        scanf("%s",Password);
        printf("\nCompare Login = %d Compare Login = %d\n",strcmp(buf,Login),strcmp(buf2,Password));
        n--;
        b=strcmp(buf,Login);
        v=strcmp(buf2,Password);

        if (v==0&&b==0){
            printf("\nYou logged in\n\n");
            break;
        }
        else if (n==0) {
            printf("\nYou failed\n\n");
        }
    }
    while (n!=0);
}
  • 2
    Take the [tour], read [Ask], and [MCVE]. What exactly does your program do and what do you expect it to do? Your question seems to have translated poorly. Can you please explain what it is you still need to accomplish? – jwdonahue Jul 02 '18 at 19:53
  • 1
    I honestly cannot understand your question. Can you try to reformulate it in another way? – myradio Jul 02 '18 at 21:03
  • OT: when calling the C library functions: `fopen()`, `fgets()`, always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Jul 02 '18 at 22:09
  • for ease of readability and understanding: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 2) variable (and parameter) names should indicate `content` or `usage` (or better, both). 3) separate code blocks: `for` `if` `else` `while` `do...while` `switch` `case` `default` via a single blank line. – user3629249 Jul 02 '18 at 22:13
  • when calling any of the `scanf()` family of functions: 1) always check the returned value (not the parameter value) to assure the operation was successful. 2) when using the format specifier '%s' or '%[...]' always include a max characters modifier that is one less than the length of the input buffer to avoid any possibility of a buffer overflow and the resulting undefined behavior – user3629249 Jul 02 '18 at 22:15
  • regarding: `printf("%s",buf);` this will not immediately be output to your terminal (because stdout is buffered) strongly suggest: `printf("%s\n",buf);` as the '\n' will force the stdout buffer to be flushed to the terminal – user3629249 Jul 02 '18 at 22:19
  • @user3629249 I doubt they understand what your saying. They seen to be new to C (and programming in general). – Krii Jul 02 '18 at 22:24
  • regarding: `printf("\nCompare Login = %d Compare Login = %d\n",strcmp(buf,Login),strcmp(buf2,Password));` 1) there is no horizontal spacing, so it is difficult to read. 2) it does not honor the right margin of the printed page 3) it does not help the user, save it for when debuggin – user3629249 Jul 02 '18 at 22:25
  • Possible duplicate of [How to extract a substring from a string in C?](https://stackoverflow.com/questions/19555434/how-to-extract-a-substring-from-a-string-in-c) – Krii Jul 02 '18 at 22:48
  • `buf` has a `'\n'` in it. `Login` will never have a `'\n'` in it. Tip: never use `scanf()`. – chux - Reinstate Monica Jul 03 '18 at 01:31

1 Answers1

1

It sounds like your asking how to get both your username and password from the same file. Yes?

In order to extract the username and password from a single string (buf), you need to use a delimiter to separate them. Let's say Login.txt looks like this:

MyUsername1
MyPassword1
MyUsername2
MyPassword2

In this case, the delimiter is the line ending characters (\r\n on Windows).

You need split the string stored in buf apart using, for example, strtok or strsep (see an example).

It's going to take some learning on your part to figure out exactly how to do it. If I just type up a fully functioning program for you, you won't learn anything; so I'm not going to do that.

Krii
  • 907
  • 9
  • 23