Use arrays to search accounts and verify passwords from files

Source: Internet
Author: User

Use arrays to search accounts and verify passwords from files
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: Use fscanf To Read File Information. 1 char a [20]; 2 3 FILE * fp; 4 fp = fopen ("test.txt", "rb"); 5 fscanf (fp, "% s", ); 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; 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. With the above Code, the result will be: the character array a is abcABC, and the 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 and then execute the following code: copy the 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); fclose (fp); return 0;} are omitted here. The result of copying the code is: the character array a is abc, B is ABC and c is ruby. Step 2: Search for the Account to retrieve the file data. Find the account that matches the input and 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: copy the code 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); copy the code and use strcmp (str1, str2) to determine the data is the same ); if the two character arrays store the same content, strcmp (str1, str2) = 0 stops searching and uses break; jumps out of the loop. Here is an example: 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 the structure is used, when the account is detected, use the structure ".", that is, "point", to solve the problem. 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: copy the Code if (strcmp (shuRu, a) = 0) {flag = 1; fscanf (fp, "% s", a); fscanf (fp, "% s", a); break;} copy the code. When an account is detected, the flag is 1, indicating that the 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 I will explain why two fscanf () are used; the first fscanf () is to assign the first data under the account to the character array a; the second fscanf (); the second data under the Account is assigned to the character array a; because the password data is the second data under the account data, two fscanf () must be used; Because fscanf (); it is read in sequence and cannot skip. Step 4: Verify the password and locate the password data. Then 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: copy the code void checkKey (char a [20]) {char mima [20]; cout <"Enter Password:"; cin> mima; if (strcmp (a, mima) = 0) cout <"Logon successful" <endl; else {cout <"Incorrect password! Enter again. "<Endl; Sleep (2000); checkKey (a);} return;} copy the code Step 5: The demo has initially completed this function, and now the complete code is pasted. Copy 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} copy the code and run it. First, the file data is as follows: (the third behavior account of each account and the fifth behavior password) running result: Step 6: bug fixes seem to have completed the expected functions, but observe carefully, it is not difficult to find a bug. For example: When the file content is: we can see that the password of the first account is the same as that of the second account, both are aabbcc. At this time, the program runs incorrectly. When aabbcc is found, the program regards him as an account, so 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. after the account is registered and the information is written into the file, append a "@" 2 to the account. when logging on to the system, after entering the account, append a "@" to the input account, which is not the scope of our discussion, let's take a look at the second part of the article to be modified: The related function: strcat (str1, str2); to append the character array str2 to the end of str1. The Code is as follows: cout <"Enter the account name:"; cin> shuRu; strcat (shuRu, "@"); so when you log on, when you 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 modified code is as follows: Copy 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 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} the password is successfully copied. 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.

Related Article

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.