我在我的程序上实现登录功能时遇到了问题.... [英] I got a problem in implementing login function on my program....

查看:58
本文介绍了我在我的程序上实现登录功能时遇到了问题....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我正在编写一个代码,用于验证纯文本文件(user.txt)中的id和密码



i想通过从user.txt读取数据来输入程序检查ID,但我遇到了问题。



例如:



ID:louis

pwd:lenox



错误的ID!



这些id和pwd肯定在user.txt中,但是我的程序无法读取它。

我会向你显示我的代码,有人在我的代码中找到错误修复它,我会非常感激。

- 头文件 -

Dear guys,

I'm writing a code which validates id and password from plain-text file(user.txt)

i wanted to make program check id after i typed it by reading data from user.txt but I had a problem.

example:

ID: louis
pwd:lenox

incorrect id!

these id and pwd surely is in user.txt but my program couldn't read it.
I'll show my code to you and someone pick up the error in my code to fix it, I would appreciate it very much.
- header file -





#pragma once
#define _CRT_SECURE_NO_WARNINGS
#ifndef COMMON_H
#define COMMON_H

#include <iostream>
#include <memory>
#include <fstream>
#include <string>
#include <iomanip>
#include <conio.h>

using namespace std;
const char* userDB = "user.txt";//contains user info
const char* recordDB = "record.txt";//contains game record
const char* help = "help.txt";

#define MAXLENGTH    8
#define MAXNUM       3
#define MAXTRIALS    3

struct loginfo
{
	string id;
	string pwd;
	friend istream &operator >> (istream& is, loginfo l)
	{
		return is >> l.id >> l.pwd;
	}
};
//password masking
string setpass(const char* prompt, bool show_asterisk = true)
{
	const char BACKSPACE = 8;
	const char RETURN = 13;
	string pass;
	unsigned char c = 0;
	cout << prompt;

	while ((c = _getch()) != RETURN)
	{
		if (c == BACKSPACE)
		{
			if (pass.length() != 0)
			{
				if (show_asterisk)
					cout << "\b \b";
				pass.resize(pass.length() - 1);
			}
			else if (c == 0 || c == 224)
			{
				_getch();
				continue;
			}
		}
		else
		{
			pass.push_back(c);
			cout << '*';
		}
	}
	cout << endl;
	return pass;
}
//check whether userDB file exist or not
bool isExist(const char* f)
{
	fstream in;
	in.open(f, ios::in | ios::binary);

	if (in.fail())
		return true;
	else
		return false;
}
//check continue signup or not
bool isContinue()
{
	char m;
	cout << "Continue?(Yes = y,No = n)";
	cin >> m;
	if (m == 'y')
		return true;
	else if (m == 'n')
		return false;
}
//main menu screen
int mainMenu()
{
	int ch;
	cout << "================" << endl;
	cout << "   Welcome!!!   " << endl;
	cout << "  1. signup     " << endl;
	cout << "  2. login      " << endl;
	cout << "  3. record     " << endl;
	cout << "  4. help       " << endl;
	cout << "  5. exit       " << endl;
	cout << "================" << endl;
	cout << "your choice:";
	cin >> ch;
	return ch;
}
#endif




#pragma once
#ifndef SIGNUP_H
#define SIGNUP_H

#include "Common.h"


class player
{
private:
	string id;
	string pwd;
public:
	player() { id = ' '; pwd = ' '; }//constructor for signup
	loginfo input();//input user info(signup)
	void saveinput(bool c, loginfo l,const char* fi);//save input into file
	void confirm(const char* f);//check saved data
};
//input user info 
loginfo player::input()
{
	auto l = make_unique<loginfo>();//smart pointer
	string i,p,vp;
	int cnt = 0;
	fstream in;
	in.open(userDB, ios::in|ios::binary);

	cout << "====================" << endl;
	cout << "    signup          " << endl;
	cout << "    ID: ";
	cin >> i;
	//check id length(up to 8 characters.)
	if (i.length() > MAXLENGTH)
	{
		cout << "ID may have up to 8 characters !" << endl;
		i.clear();
		cout << "    ID: ";
		cin >> i;
	}
	//id duplication check
	while (in>>l->id>>l->pwd)
	{
		if (l->id == i)
		{
			cout << "duplicate id!" << endl;
			cout << "    ID: ";
			cin >> i;
		}
	}
	//check duplicate id
	l->id = i;
	p = setpass("   password:");//password masking
	if (p.length() > MAXLENGTH)//check password's length
	{
		cout << "Password may have up to 8 characters !" << endl;
		p.clear();
		p = setpass("   password:");
	}
	vp = setpass("   verify password: ");//password masking 
	while (p != vp)//when user-input is wrong
	{
		cout << "input error!" << endl;
		cnt++;
		if (cnt >= MAXNUM)//when user fail three times to verify password
		{
			cout << "failed to veify!" << endl;
			cout << "please start over!" << endl;
			exit(0);
		}
		vp = setpass("   verify password: ");	
	}
	l->pwd = p;
	return *(l);
}
//save input
void player::saveinput(bool c, loginfo l,const char* fi)
{
	fstream f;
	if (c == true)
	{
		f.open(fi, ios::out | ios::binary);
		if (!f)
		{
			cout << "failed to open!" << endl;
			exit(0);
		}
		else
		{
			cout << "saving..." << endl;
			f << l.id << " " << l.pwd << endl;
			cout << "registration complete." << endl;
			f.close();
		}
	}
	else
	{
		f.open(fi, ios::out | ios::app | ios::binary);
		if (!f)
		{
			cout << "failed to open!" << endl;
			exit(0);
		}
		else
		{
			cout << "appending..." << endl;
			f << l.id << " " << l.pwd << endl;
			cout << "successfully appended." << endl;
			f.close();
		}
	}	
}
//check whether signup succeed or not
void player::confirm(const char* f)
{
	loginfo l;
	fstream in;
	in.open(f, ios::in | ios::binary);

	if (!in)
	{
		cout << "failed to read!" << endl;
		exit(0);
	}
	while (in >> l.id >> l.pwd)
		   ;
	cout << "player info: " << endl;
	cout << "Id: " << l.id << "," << "Password:" << l.pwd << endl;
}

void showAll()
{
	auto l = make_unique<loginfo>();
	fstream in;
	in.open(userDB, ios::in);

	while (in>>l->id>>l->pwd)
	{
		cout << l->id << " " << l->pwd << endl;
	}
}
#endif




#include "signup.h"

int main(void)
{
	//initialization
	bool check = isExist(userDB);
	bool con = true;//signup continue?(default = true)
	string id, pwd;//for login
	int selection;//select menu

	selection = mainMenu();//call main menu

	switch (selection)
	{
	case 1:
		//signup
		while (con)
		{
			auto p = make_unique<player>();
			loginfo ll;

			ll = p->input();
			p->saveinput(check, ll, userDB);
			p->confirm(userDB);

			con = isContinue();
		}
		break;
	case 2:
		auto l = make_unique<loginfo>();//instantiate structure for user info
		fstream in;
		in.open(userDB, ios::in | ios::binary);//open the text file contains info

		cout << "id:";
		cin >> id;

		while (in >> l->id >> l->pwd)//read user info from text file
		{
			//to check id's correct or not
			if (l->id == id)//id match
			{
				pwd = setpass("password: ");//password masking
				if (l->pwd == pwd)//password match
				{
					cout << "user verified!" << endl;
					break;
				}
				else  //password mismatch
				{
					cout << "incorrect password." << endl;
					pwd = setpass("password:");//pwd reinput
				}
			}
			else
			{
				cout << "incorrect id" << endl;
			}
		}
		break;
	}
	system("pause");
	return 0;
}





我的尝试:



我写了一个代码来测试我的数据文件的完整性如下;



auto l = make_unique< loginfo>();

fstream in;

in.open(userDB,ios :: in | ios :: binary);



cout< < id:;

cin>> id;



while(>> l-> id>> l-> pwd)

{

if(l-> id == id)

cout<< l-> id<< << l-> pwd<<结束;

}



此代码完全符合我的预期。这让我非常困惑......



What I have tried:

I wrote a code to test my data file's integrity as follows;

auto l = make_unique<loginfo>();
fstream in;
in.open(userDB, ios::in | ios::binary);

cout << "id:";
cin >> id;

while (in >> l->id >> l->pwd)
{
if (l->id == id)
cout << l->id << " " << l->pwd << endl;
}

this code perfectly worked as i intended. this made me very confused...

推荐答案

你必须提供一个完整的路径,否则文件必须与你的目录在同一个目录中exe运行。并且:您的进程需要该文件的读取或写入权限。



请记住:密码非常敏感,因此需要进行密码加密。一种好方法也是只存储一个SHA-1哈希值。
You must provide a full path, else the file MUST be in the same directory as your exe runs. And: your process needs the read or write rights for that file.

Remember: passwords are extremly sensitive, so they need to be securly encrypted. A good approach is also to store only a SHA-1 hash value.


这篇关于我在我的程序上实现登录功能时遇到了问题....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆