Use arrays to search accounts and verify passwords from files, and use arrays to verify passwords.

Source: Internet
Author: User

Use arrays to search accounts and verify passwords from files, and use arrays to verify passwords.

 

Recently, I was writing a bank management system and asked me how to search my account from a file. After I gave the method, I didn't know how to match the password in the file with the input. In general, an efficient method is to use a linked list. Array implementation is not an efficient method, and it wastes space. Furthermore, for accounts with a collection of personal information, the structure is generally used, and the code is easy to write.

However, it uses arrays instead of struct. In this case, how do I search for an account and verify the password? I tried it and found it difficult to solve it.

Solution: sort and locate the file data.

Step 1: Read File Information

Use fscanf To Read File Information.

1 char a[20];2 3 FILE* fp;4 fp = fopen("test.txt","rb");5 fscanf (fp, "%s", a);6 fclose(fp);

Fcanf reads data, separated by spaces. For example, if the file content is "abc ABC" (within double quotation marks), use the following code:

 

fscanf (fp, "%s", a);fscanf (fp, "%s", b);

 

The character array a is abc, and the character array B is ABC;

If the file content is "abcABC" (within double quotation marks), that is, there is no space between abc and ABC. Use the above Code and the result will be:

Character array a is abcABC; character array B is garbled.

Fscanf (); another feature is that a program reads data sequentially.

In this example:

Txt file (one row of data) content:

Abc

ABC

Ruby

 

Then execute the following code:

Int main () {char a [20], B [20], c [20]; FILE * fp; fp = fopen ("test.txt", "rb "); fscanf (fp, "% s", );//...... N lines of code fscanf (fp, "% s", B) are omitted here );//...... N lines of code fscanf (fp, "% s", c) are omitted here; fclose (fp); return 0 ;}

 

The result is:

The character array a is abc, B is ABC, and c is ruby;

 

Step 2: Search for accounts

 

 

The account search method is to traverse the file data and find the account that matches the input to stop searching.

Use while (fscanf (fp, "% s", a)> 0) to traverse file data.

The function is to read one row of the file content and assign it to character array a, and then compare it with the input account.

At the same time, flag is used to determine whether a matched account is found, so that different situations can be processed in the future.

The Code is as follows:

 

 

Char a [20]; char shuRu [20] = {'\ 0'}; // enter int flag = 0; cout <"Enter the account name :"; cin> shuRu; FILE * fp; fp = fopen ("test.txt", "rb"); while (fscanf (fp, "% s", a)> 0) // traverse file data {if (strcmp (shuRu, a) = 0) {cout <"find matched account" <endl; flag = 1; break ;}} if (flag = 0) {cout <"the user does not exist. Please register! "<Endl; // next} else // next; fclose (fp );

 

The Code uses strcmp (str1, str2) to determine that the data is the same. If the two character arrays store the same content, strcmp (str1, str2) = 0

In this case, the search is stopped and break is used to jump out of the loop.

Here is an example:

Among them, the third behavior account and the fifth behavior password. Others include name, address, age, and other information.

Run the following command:

 

Step 3: locate the File Password data

If a struct is used, when an account is detected, it is solved by the struct's ".", that is, the "dot" password. Simple and Convenient. However, it is not a struct, so only other methods can be used. How can I achieve this by using "sorting and positioning" at the beginning of the essay?

From the data in this file, the account and password are respectively the third line, the fifth line, and the fourth line is separated. The other accounts below are also sorted in the same order.

Locate the fifth line, and then compare the password with the file data.

The Code is as follows:

 

if (strcmp(shuRu,a) == 0){    flag = 1;    fscanf(fp,"%s",a);    fscanf(fp,"%s",a);    break;}

 

When the account is detected, flag = 1; indicates that a matched account is found.

Then two fscanf (fp, "% s", a) are used. This is not a code error. As mentioned above, fscanf (); is read sequentially and a, B, example of a three-character array in c.

Here we will explain why two fscanf ();

The first fscanf () is to assign the first data under the account to the character array;

The second fscanf () is to assign the second data under the account to the character array;

Because the password data is the second data under the account data, you must use two fscanf (); Because fscanf (); is read in sequence and cannot skip.

 

Step 4: Verify the password

After locating the password data, you can verify the password. In order to re-enter the password when the password is incorrect, we must write the verification password step as a function and call it cyclically, similar to recursive usage.

The Code is as follows:

 

Void checkKey (char a [20]) {char mima [20]; cout <"input password:"; cin> mima; if (strcmp (a, mima) = 0) cout <"Logon successful" <endl; else {cout <"Incorrect password! Enter again. "<Endl; Sleep (2000); checkKey (a) ;}return ;}

 

 

Step 5: run the demo

After completing this function, we can paste the complete code.

 

1 # include <iostream> 2 # include <cstdlib> 3 # include <cstring> 4 # include <windows. h> 5 using namespace std; 6 7 // check password 8 void checkKey (char a [20]) 9 {10 char mima [20]; 11 cout <"Enter password: "; 12 cin> mima; 13 if (strcmp (a, mima) = 0) 14 cout <" Logon successful "<endl; 15 else16 {17 cout <"Incorrect password! Enter again. "<Endl; 18 Sleep (2000); 19 checkKey (a); 20} 21 22 return; 23} 24 25 int main () 26 {27 char a [20]; 28 char shuRu [20] = {'\ 0'}; 29 30 int flag = 0; 31 32 cout <"Enter the account name:"; 33 cin> shuRu; 34 35 FILE * fp; 36 fp = fopen ("test.txt", "rb"); 37 38 while (fscanf (fp, "% s", a)> 0) 39 {40 41 if (strcmp (shuRu, a) = 0) 42 {43 flag = 1; 44 fscanf (fp, "% s", ); 45 fscanf (fp, "% s", a); 46 break; 47 48} 49 50} 51 if (flag = 0) 52 cou T <"the user does not exist. Please register! "<Endl; 53 else54 checkKey (a); 55 56 fclose (fp); 57 return 0; 58}

 

Let's run it.

First, the file data is as follows: (the third behavior account and the fifth behavior password for each account)

Running result:

 

Step 6: fix bugs

It seems that the expected functions have been completed, but it is not difficult to find a bug. For example:

When the file content is:

It can be seen that the password of the first account is the same as that of the second account, both of which are aabbcc. At this time, the program runs incorrectly. When aabbcc is found, the program regards it as an account, an error occurs.

Fixing the bug is also very easy. It uses a special character to process the account. For example, append a "@" to the account.

Therefore, the program needs to modify two places

1. When the information is written to a file after the account is registered, append a "@" to the account

2. When logging on to the system, after entering the account, append a "@" to the input account.

The first part is not the scope of our discussion. Let's take a look at the second part of the article to be modified:

Related functions: strcat (str1, str2); append str2 to str1.

The Code is as follows:

 

Cout <"Enter the account name:"; cin> shuRu; strcat (shuRu ,"@");

 

Therefore, when you log on, enter the account and press the Enter key, the program will automatically append a character "@" to the account you entered, and then compare it with the file data.

The complete code after modification is as follows:

 

1 # include <iostream> 2 # include <cstdlib> 3 # include <cstring> 4 # include <windows. h> 5 using namespace std; 6 7 // check password 8 void checkKey (char a [20]) 9 {10 char mima [20]; 11 cout <"Enter password: "; 12 cin> mima; 13 if (strcmp (a, mima) = 0) 14 cout <" Logon successful "<endl; 15 else16 {17 cout <"Incorrect password! Enter again. "<Endl; 18 Sleep (2000); 19 checkKey (a); 20} 21 22 return; 23} 24 25 int main () 26 {27 char a [20]; 28 char shuRu [20] = {'\ 0'}; 29 30 int flag = 0; 31 32 cout <"Enter the account name:"; 33 cin> shuRu; 34 strcat (shuRu, "@"); 35 36 FILE * fp; 37 fp = fopen ("test.txt", "rb"); 38 39 while (fscanf (fp, "% s", a)> 0) 40 {41 42 if (strcmp (shuRu, a) = 0) 43 {44 flag = 1; 45 fscanf (fp, "% s", a); 46 fscanf (fp, "% s", a); 47 break; 48 49} 50 51} 52 If (flag = 0) 53 cout <"the user does not exist. Please register! "<Endl; 54 55 else56 checkKey (a); 57 58 fclose (fp); 59 return 0; 60}

 

File data:

Running result:

The password is successfully matched.

Summary: for programs that do not use linked list + struct to write account registration, it is purely file information processing. Therefore, we can only solve the problem in some unsatisfactory ways. It uses the "sort and locate" method.

Code is efficient and concise. In the beginning, there was no good solution. Although it could also lead to Rome, it is not recommended from the perspective of program maintenance and update.

 


How to use asp to traverse arrays to achieve multi-password login for a single user

The first floor does not seem to understand the question ..

Set: The obtained account variable is u password variable is p

....
SQL = "select user, pass from admin where user = '" & u &"'"
Set rs = conn.exe cute (SQL)

If rs. eof then
Response. write ("this user cannot be found! ")
Response. end
End if

Dim check, arr
Check = false
Arr = split (rs ("pass"), "| ")
For I = 0 to ubound (arr)
If p = arr (I) then
Check = true
Exit
End if
Next

If not check then
Response. write ("Incorrect password ")
Response. end
End if

'Here is what is executed after account and password are passed ........

How does java use arrays to receive user input numbers and output arrays?

Public class Util {public static void main (String [] args) {java. util. required SC = new java. util. using (System. in); String [] arr = new String [5]; for (int I = 0; I <arr. length; I ++) {arr [I] = SC. next () ;}// util is used here. arrays code output array System. out. println (java. util. arrays. toString (arr ));}}
Sun Cheng [authoritative expert]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.