MFC关于实现小程序登陆注册功能的实现

https://blog.csdn.net/DCGJ666/article/details/82467961

MFC关于实现小程序登陆注册功能的实现


1.首先我们在头文件和源文件中分别添加InfoFile.h文件和InfoFile.cpp文件,用来进行数据的读写操作。

// 注册窗口的变量声明
private:
    CString m_register1; //注册窗口用户名edit控件变量
    CString m_register2; //注册窗口密码edit控件变量
    CString m_register3; //注册窗口确认密码edit控件变量
1
2
3
4
5
6
//InfoFile.h文件的具体代码
#pragma once

#include <list>
#include <fstream>
#include <iostream>
#include <string>

#define F_LOGIN "./login.ini"

using namespace std;

class InfoFile
{
public:
    InfoFile();
    ~InfoFile();

    void writeRegister(char* name, char* pwd); //将注册信息写入操作
    void ReadRegister(CString &name, CString &pwd); //从文件中读取用户名密码的操作
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//InfoFile.cpp的具体代码
#include "stdafx.h"
#include "InfoFile.h"
InfoFile::InfoFile()
{
}

InfoFile::~InfoFile()
{
}
void InfoFile::ReadRegister(CString &name, CString &pwd)
{
    ifstream ifs;
    ifs.open(F_LOGIN); //打开文件

    char buf[1024] = {0};

    ifs.getline(buf, sizeof(buf));//读取用户名信息
    name = CString(buf);

    ifs.getline(buf, sizeof(buf));//读取密码信息
    pwd = CString(buf);

    ifs.close();
}

void InfoFile::writeRegister(char* name, char* pwd)
{
    ofstream ofs;
    ofs.open(F_LOGIN);

    ofs<<name<<endl;//写入用户名信息
    ofs<<pwd<<endl;//写入密码信息
    ofs.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


2.进入注册对话框的确定按钮进行函数描述

void CD_register::OnBnClickedButton3()
{

    UpdateData(TRUE);//更新变量信息
    InfoFile file;
    CStringA tmp;
    CStringA tmp2;

    if(m_register2 != m_register3)
    {
        MessageBox(_T("Inconsistent password!"));
    }
    else
    {
        tmp = m_register1;
        tmp2 = m_register2;
        file.writeRegister(tmp.GetBuffer(), tmp2.GetBuffer());//将char类型转化为CString,并进行用户名和密码的写入
        MessageBox(_T("Register success!"));
    }
    // TODO: Add your control notification handler code here
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
3.进入登陆界面进行函数配置 


UpdateData(TRUE);

    InfoFile file;
    CString name, pwd;
    file.ReadRegister(name, pwd);
    if(name == m_UserName)
    {
        if(pwd == m_UserPassword)
        {
            CD_story dlg;
            dlg.DoModal();//打开登录后的界面
        }
        else
        {
            MessageBox(_T("UserPassword is error!"));
        }
    }
    else
    {
        MessageBox(_T("UserName is error!"));
    }

 

猜你喜欢

转载自blog.csdn.net/active2489595970/article/details/88292342