如何从C[保持]中的单个文件中提取多个字符串

时间:2021-11-08 01:36:12

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 DavidPeter

看看我下面的代码。这是登录验证的标准程序。在文件”登录。txt"位于一个用户名的字符串。当程序开始时,它要求输入用户名,然后将这个字符串与“Login”中保存的字符串进行比较。txt,如果两者相等——成功,如果不相等,那么我们只能尝试3次,直到程序结束;我需要保存在“Login”中。txt" 3字串(用户名),例如:Brad DavidPeter

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;

所以我需要调用“Login”中的每个字符串。将txt分别调用char buf[],然后调用scanf函数(David -例如),在对两者都使用strcmp后,两个字符串应该是相等的;

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

问:如何“单独”提取多个字符串,并将每个字符串从C文件中的单个文件中放入不同的char buf[] ?

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);}

1 个解决方案

#1


0  

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:

为了从单个字符串(buf)中提取用户名和密码,需要使用分隔符来分隔它们。假设登录。txt看起来像这样:

MyUsername1MyPassword1MyUsername2MyPassword2

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

在本例中,分隔符是行结束字符(Windows上的\r\n)。

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

您需要使用strtok或strsep将存储在buf中的字符串分开(请参见示例)。

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.

要想搞清楚具体怎么做,你需要学习一下。如果我为你输入一个功能齐全的程序,你什么也学不到;我不打算这么做。

#1


0  

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:

为了从单个字符串(buf)中提取用户名和密码,需要使用分隔符来分隔它们。假设登录。txt看起来像这样:

MyUsername1MyPassword1MyUsername2MyPassword2

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

在本例中,分隔符是行结束字符(Windows上的\r\n)。

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

您需要使用strtok或strsep将存储在buf中的字符串分开(请参见示例)。

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.

要想搞清楚具体怎么做,你需要学习一下。如果我为你输入一个功能齐全的程序,你什么也学不到;我不打算这么做。