C++离线存储登录系统

这是一个使用C++编写的简单用户登录注册系统,通过文本文件存储用户信息。程序包括登录和注册两个功能,支持账号密码的验证、修改和注销。利用了文件流操作,如ios::in、ios::out、ios::app等标志来读写文件。

直接上代码,

#include<iostream>
#include<fstream>
#include <string>
#include <windows.h>
#include<conio.h>
using namespace std;
void denglu() {
	while(1) {
		system("cls");
		int sr;
		cout << "登录"<<endl;
		cout << "尚未注册(1)"<<endl;
		cout << "已注册,登录(2)"<<endl;
		sr=getch();
		if(sr=='1') {
			break;     //跳出该循环
		}
		system("cls");
		string zh,mm,u1,u2;
		cout << "账号:";
		cin >> zh;
		cout << "密码:";
		cin >> mm;
		ifstream ifs;
		ifs.open("yhzl.txt", ios::in);
		while (ifs >> u1 && ifs >> u2) {
			if (u1 == zh && u2 == mm) {
				cout << u1 << " 你好" << endl;
				cout<<endl;
				cout<<"修改密码(1)"<<endl;
				cout<<"注销账号(2)"<<endl;
				cout<<"退出登录(3)"<<endl;
				sr=getch();
				if(sr=='1') {
					system("cls");
					string x,y;
					cout << "原密码:";
					cin >> x;
					if(x==u2) {
						ofstream oof;
						oof.open("temp.txt", ios::binary | ios::out);     //temp.txt用于交换
						cout << "新密码:";
						cin >> y;
						oof << u1 << "\t" << y << endl;
						oof.close();
						ofstream ofs;
						ifstream iof;
						ofs.open("yhzl.txt", ios::binary | ios::out);
						iof.open("temp.txt", ios::binary | ios::out | ios::in);
						while (iof >> u1 >> y ) {
							ofs << u1 << "\t" << y << endl;
						}
						cout << "修改成功,请重新登录"<<endl;
						system("pause");
						ofs.close();
						iof.close();
						continue;
					} else if(x!=u2) {
						cout << "原密码错误,请重新登录"<<endl;
						system("pause");
						continue;
					}
				}
				if(sr=='2') {
					cout << "确认注销"<<endl;
					cout << "确认(1)"<<endl;
					cout << "取消(2)"<<endl;
					sr=getch();
					if(sr=='1') {
						ifstream ifs;
						ofstream oof;
						ifs.open("yhzl.txt", ios::binary | ios::out | ios::in);
						oof.open("temp.txt", ios::binary | ios::out);
						while (ifs >> u1 && ifs >> u2 ) {
							if (u1 == zh ) {
								continue;
							}
							oof << u1 << "\t" << u2 <<endl;
						}
						ifs.close();
						oof.close();
						ofstream ofs;
						ifstream iof;
						ofs.open("yhzl.txt",ios::binary | ios::out);
						iof.open("temp.txt", ios::binary | ios::out | ios::in);
						while (iof >> u1 >> u2 ) {
							ofs << u1 << "\t" << u2 << endl;
						}
						ofs.close();
						iof.close();
					}
					if(sr=='2') {
						continue;
					}
				}
				if(sr=='3') {
					system("cls");
					cout<<"已退出登录"<<endl;
					system("pause");
					break;
				}
			} else if(u1 != zh || u2 != mm) {
				cout<<"账号或密码错误"<<endl;
				system("pause");
				break;
			}
		}
		ifs.close();
	}
}
void zhuce() {
	int sr,x=0;
	while(1) {
		x=0;
		string zh,mm,zhe;
		system("cls");
		cout << "账号:";
		cin >> zh;
		ifstream ifs;
		ifs.open("zh.txt", ios::in);
		while (ifs >> zhe ) {
			if (zh == zhe) {
				cout << "已有账号,是否登录?" << endl;
				cout<<endl;
				cout<<"是(1)"<<endl;
				cout<<"否(2)"<<endl;
				sr=getch();
				if(sr=='1') {
					x=1;
					break;

				}
				if(sr=='2') {
					x=2;
					break;
				}
				if(sr != '1' || sr != '2') {
					x=1;
					break;
				}
			} else {
				break;
			}
		}
		if(x==1) {
			break;
		}
		if(x==2) {
			system("cls");
			continue;      //跳出本次循环
		}
		ifs.close();
		cout << "密码:";
		cin>>mm;
		ofstream ofs;
		ofs.open("yhzl.txt", ios::out|ios::app);                       //若是电脑读取不了中文的,只能用首字母或数字代替。
		ofs << zh << "\t" << mm << endl;        //这里的“\t”表示空格,电脑会自动为我们把数据对齐
		ofs.close();
		ofs.open("zh.txt", ios::out|ios::app);                       //应该是我技术问题,若是会单项查找,可以自己尝试
		ofs << zh << endl;
		ofs.close();
		cout << "注册成功" <<endl;
		system("pause");
		break;
	}
}
int main() {
	system("chcp 65001");
	ofstream ofs;
	ofs.open("yhzl.txt", ios::out|ios::app);   //创建txt文件
	ofs.close();
	ofs.open("zh.txt", ios::out|ios::app);     //zh.txt用于存储账号,yhzl.txt则用于存储用户信息
	ofs.close();
	while(1) {
		denglu();
		zhuce();
	}
	return 0;
}

这系统的是用txt文件存储的,

先了解一下文件流,

ofs.open("yhzl.txt", ios::binary | ios::out);

 ofs.open是打开,并以某种方式打开,

某种方式取决与ios::后面的代码,向上面的binary,out,还有in, app,ate等

ios是关联 ifstream的,

可以这样引用ifstream

#include<fstream>

#include<iostream> 

<fstream> 就是ofs的头文件,而<iostream> 是常见输出输入的头文件,如cout,cin

ios::后面的代码意思就像下面这表格

代码用途
in读取txt
out写入txt
app输入标全部跳到最后面
ate打开输入标跳到最后面
binary用二进制打开
trunc把之前写的扔掉

 

ofs.open("yhzl.txt", ios::binary | ios::out);

 这个就是,啊

先不说,

你猜

答案在这     答案

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

你猜

答案

以二进制写的方式打开yhzl.txt

我爱bug,快说说bug吧

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值