C language How to write the user account password to the file to implement login registration function?

Source: Internet
Author: User
Tags fread strcmp

Create a file that stores your user account password:

voidCreate_file () {File*FP; if(fp = fopen ("Users.txt","RB")) ==null)/*If this file does not exist*/    {        if(fp = fopen ("Users.txt","wb+"))==NULL) {Outtextxy ( -, $,"Unable to build file!"); }    }}


About file read and write:

R     Read    -only for input open a text file (the file must exist) W     write only    as output open a text file (if no file, then new) a     append    data to the end of the text file (if no file, then new) RB    Read    -only open a binary file for input WB    write    as output open a binary file ab    append    to the end of the binary file add data R+     read/write    Open a text file for read/write (file must already exist) W+ read/write     for read/write    create a new text file a+     read/write    Open a text file for read/write (read-write pointer positioned at last) RB+ read/write     open a binary filefor read/write for read/write     Create a new binary file ab for reading and writing +   Read/write    open a binary file for read-write


To create a struct body:

struct The_users   //typedef can define struct-body aliases {    char id[one by one];     Char pwd[];} Users;

The structure is used to store user account passwords.

Registered user (account password)

     voidRegisters ()//the size of the structure, write or read into 1 structure sizes at a time{users A, B;//struct the_users Rename definitionFILE *FP; FP= fopen ("Users.txt","R"); Fread (&b,sizeof(structThe_users),1, FP);//read into a struct word converts sequential blocks to BInputBox (a.id, One,"Please enter your account number");  while(1)         {                 if(strcmp (a.ID, b.id))/*if two strings are not equal*/             {                 if(!feof (FP))/*If the end of the file is not*/{fread (&b,sizeof(structThe_users),1, FP); }                 Else                      Break; }             Else{outtextxy ( -, $,"This user name already exists!");                 Fclose (FP);                  Main (); }} InputBox (A.pwd,Ten,"Please enter your password"); FP= fopen ("Users.txt","a"); Fwrite (&a,sizeof(structThe_users),1, FP); Outtextxy ( -, $,"account registration is successful!");         Fclose (FP);     Main (); } 


Login account Password:

voidInput_login () {users a,b;//define struct the_users alias FILE*FP; FP= fopen ("Users.txt","R"); Fread (&b,sizeof(structThe_users),1, FP);//read into a struct character block write BInputBox (a.id, One,"Please enter your account number"); Outtextxy (295, the, a.id);  while(1)           {              if(strcmp (a.id, b.id) = =0)//If you have this user name           {                Break; }                      Else            {               if(!feof (FP))//If the file is not finished reading{fread (&b,sizeof(structThe_users),1, FP); }                              Else               {               Outtextxy ( -, $,"This user name does not exist!");                   Fclose (FP);                                  Main (); }}} InputBox (A.pwd, -,"Please enter your password"); if(strcmp (a.pwd, b.pwd) = =0)/*if the password matches*/{fclose (FP); Outtextxy ( -, $,"Login successful, Welcome to use!");           }       Else         {   Outtextxy ( -, $,"The password is not correct!");       Main (); }       }

C language How to write the user account password to the file to implement login registration function?

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.