txt文件里的数字乱码

img

img


我存到txt文件里后数字就乱码了,编码方式用的是utf-8,一般不都是汉字乱码吗?奇怪……
代码
book.h
#pragma once//图书管理模块
class data {public: virtual void bj() = 0;
virtual void sc() = 0;
virtual void xs() = 0;};
//图书类
class book {public: int bianhao; //图书编号 char name[20]; //图书名称
int jiechu; //1:在架 2:借出
int shanchu; //1:存在 2:删除
public: book() { bianhao = 0; name[0] = 0; jiechu = 0; shanchu = 0; } //无参构造函数
void setbh(int n); //设置图书编号
int getbh(); //返回图书编号值
void setname(char na[]);
char* getname(); //返回图书名称
void jcbook();
void scbook();
void addbook(int bianhao, char na[]); //增加图书功能};
//图书数据库类
const int bmax = 50; //定义书本最大值
class bdata :public data {public: book bjihe[bmax]; //图书集合
int top;
public: bdata(); ~bdata(); //析构函数,将bjihe[]写到book.txt文件中
void addbooks(); //在图书数据库中添加图书 int findbook(int suffix); //查找图书,该函数用于借书操作,suffix是数组下标
int findbook(); //查找图书,该函数用于图书维护 void bj(); //编辑图书
void sc(); //删除图书
void xs(); //显示全部图书};
void bmanage(); //图书信息管理界面 声明
void jiehuan(char jh);
void yongguan();
void yonghu();
void guanli();

interface.h
#pragma once
void login();
void Main();
void bMain();
void rMain();
void UI();
void ygMain();
void yMain();
void gMain();

reader.h
#pragma once
#include"book.h"

//读书管理模块
//读者类
class reader {
public:
int bianhao;
char name[20];
int cunzai; //1存在 2删除
int jieshu; //1借过书 2未借书
book jsbook;
public:
void addreader(int id, char na[]); //增加读者
void setjs(book bt); //设置借书标记
book getjs() {
return jsbook; //返回借书信息
}
};
//读者数据库类
class rdata :public data {
public:
reader rjihe[bmax];
int top;
public:
rdata();
~rdata();
void addreaders(); //添加读者
int findreader(int suffix); //查找读者,该函数用于借书还书操作,suffix是数组下标
int findreader(); //查找读者,该函数用于读者维护操作
void bj(); //编辑读者信息
void sc(); //删除读者,读者如果借书,需要还书后才能删除
void xs(); //显示所有读者
};

void rmanage();

book.cpp
#include
using namespace std;
#include
#include

#include "interface.h"
#include "reader.h"

//图书管理模块

//图书类函数实现
void book::setbh(int n) {
bianhao = n;
}

int book::getbh() {
return bianhao;
}

void book::setname(char na[]) {
strcpy_s(name, na);
}

char* book::getname() {
return name;
}

void book::jcbook() {
jiechu = 2; //借书标志
}
void book::scbook() {
shanchu = 2; //删除标志
}

void book::addbook(int bianhao, char na[]) {
setbh(bianhao);
setname(na);
jiechu = 1;
shanchu = 1;
}

//图书数据库类
bdata::bdata() {
book b; //建立book类对象并b
top = -1; //数组下标从0开始,此处top值置为-1可保证从数组第一个元素开始存储信息
fstream file("book.txt", ios::in);//打开文件进行读操作,若文件不存在则出错.输入输出指:内存与硬盘间的信息传递
while (1) {
file.read((char*)&b, sizeof(b));//从文件中读取b个字符到char指向的缓存中(book类对象b的地址)
if (!file) //若读取到文件尾EOF,返回非0值(一般为1),结束书本数量的计数,关闭文件
break;
top++;
bjihe[top] = b;
}
file.close();
}
bdata::~bdata() {
fstream file("book.txt", ios::out);//输出:内存->硬盘
for (int i = 0; i <= top; i++) {
if (bjihe[i].shanchu == 1)
file.write((char
)&bjihe[i], sizeof(bjihe[i]));
}
file.close();
}

void bdata::addbooks() {
book bk; //创建book类对象bk
int bianhao; //书号
char bname[20]; //书名 用数组存放
cout << "请输入书号:";
cin >> bianhao;
cout << endl << "请输入书名:";
cin >> bname;
cout << endl;
bk.jiechu = 1;
bk.addbook(bianhao, bname); //类中成员函数addbook(),设置书号与书名 修改标志
top++;
bjihe[top] = bk;
return; //返回上个操作界面
}

int bdata::findbook(int suffix) {
int bianhao;
cout << "请输入书号:";
cin >> bianhao;
for (int i = 0; i <= top; i++) {
if (bjihe[i].bianhao == bianhao && bjihe[i].shanchu == 1) {
return i;
}
}
return -1;
}
int bdata::findbook() {
int bianhao;
char value[6];
cout << "请输入书号:";
cin >> bianhao;
for (int i = 0; i <= top; i++) {
if (bjihe[i].bianhao == bianhao && bjihe[i].shanchu == 1) {
if (bjihe[i].jiechu == 1)
strcpy_s(value, "在架");
if (bjihe[i].jiechu == 2)
strcpy_s(value, "借出");
cout << "书号:" << bjihe[i].bianhao << " " << "书名:" << bjihe[i].name << " " << "图书状态:" << value << endl;
return i;
}
}
return -1;

}
void bdata::bj() {
int cur;
cur = findbook();
if (cur == 1) {
cout << "该书不存在" << endl;
return;
}
cout << "当前图书信息--" << "书号:" << bjihe[cur].bianhao << " " << "书名:" << bjihe[cur].name << endl;
cout << endl;
cout << "请继续操作以修改图书信息" << endl;
cout << endl << "书名:";
cin >> bjihe[cur].name;
return;
}
void bdata::sc() {
int cur;
cur = findbook();
if (cur == 1) {
return;
}
if (bjihe[cur].jiechu == 2) { //删除时判断图书是否借出
cout << "该图书已经借出,请归还后删除!" << endl;
return;
}
bjihe[cur].shanchu = 2;
return;
}
void bdata::xs() {
char value[6];
for (int i = 0; i <= top; i++) {
if (bjihe[i].shanchu == 1) {
if (bjihe[i].jiechu == 1)
strcpy_s(value, "在架");
if (bjihe[i].jiechu == 2)
strcpy_s(value, "借出");
cout << "书号:" << bjihe[i].bianhao << "书名:" << bjihe[i].name << "图书状态:" << value << endl;
}
}
return;
}
void jiehuan(char jh) { //借书、还书函数,参数jh:判断 “借书”,“还书”. 1 借书 2 还书
int readerid = 0; //读者id
int bookid = 0; //书的id
int rsuffix, bsuffix;
bdata bd;
rdata rd;
if (jh == '1') { //借书
rsuffix = rd.findreader(readerid);
bsuffix = bd.findbook(bookid);
if (rsuffix == -1 || bsuffix == -1)
return;
if (bd.bjihe[bsuffix].jiechu == 2) {
cout << "图书已经借出,请选择其它图书" << endl;
return;
}
bd.bjihe[bsuffix].jiechu = 2;
rd.rjihe[rsuffix].jieshu = 1;
rd.rjihe[rsuffix].jsbook = bd.bjihe[bsuffix];
return;
}
if (jh == '2') {
rsuffix = rd.findreader(readerid);
bsuffix = bd.findbook(bookid);
if (rsuffix == -1 || bsuffix == -1)
return;
if (rd.rjihe[rsuffix].jsbook.bianhao == bd.bjihe[bsuffix].bianhao) {
bd.bjihe[bsuffix].jiechu = 1;
rd.rjihe[rsuffix].jieshu = 2;
rd.rjihe[rsuffix].jsbook = bd.bjihe[bsuffix];
}
else {
cout << "请重新输入,读者借阅图书书号错误" << endl;
return;
}
return;
}
}
void bmanage() {
char in;
bdata bd;
do {
system("pause");
system("cls");//切换UI时,刷新缓冲区当前内容,完成下次更新
UI();
bMain();
cin >> in;
switch (in) {
case'1':
bd.addbooks();
break;
case'2':
bd.bj();
break;
case'3':
bd.sc();
break;
case'4':
bd.findbook();
break;
case'5':
bd.xs();
break;
case'6':
break;
default:
cout << "出现错误!请重试!" << endl;
}
} while (in != '6');
return;
}
void guanli() {
char in;
do {
system("pause"); //暂停界面,以便显示所需数据
system("cls");
UI();
gMain();
cin >> in;
switch (in) {
case'1':
bmanage();
break;
case'2':
rmanage();
break;
case'3':
break;
default:
cout << "出现错误!请重试!" << endl;
}
} while (in != '3');
return;
}

interface.cpp
#include
using namespace std;
#include

void login() {
char passward[2] = "2"; //此变量用于存储登录密码
char s[2];//获取输入的密码,用于验证和登录密码是否相同
int n = 3; //限定输入错误次数是3次
cout << "请输入登录密码: ";
cin >> s;
if (!strcmp(s, passward)) { //若密码正确
cout << "恭喜您登陆成功!请稍等.\n\n";
system("pause");
}
else {
cout << "Waring!密码输入错误,请退出系统后重新尝试\n";
system("pause");
exit(0);
}
}

void ygMain() { //读管菜单
system("cls"); //清屏函数 clean screen
system("title 图书管理系统 项目开发人员 : X ");//对控制台文件名重命名
cout << "\n";
cout << "\t欢 迎 进 入 图 书 管 理 系 统\n";//与case顺序一致
cout << "\t 1. 用户 *\n ";
cout << "\t 2. 管理员 *\n ";
cout << "\t 0. 退出 * \n ";
cout << "\t\n ";
cout << " ";
cout << "\t Copyright (C) 2020-2021. All Rights Reserved \n ";
cout << "\t\n请选择您要执行的操作编号(1-2): ";
cout << endl;
void yMain() { //用户菜单
cout << "\n";
cout << "\t用户功能菜单\n";//与case顺序一致
cout << "\t 1. 借书 * \n ";
cout << "\t 2. 还书 * \n ";
cout << "\t 3. 返回上级操作 * \n ";
cout << "\t\n ";
cout << " ";
cout << "\t\n请选择您要执行的操作编号(1-3): ";
cout << endl;
}
void gMain() { //管理菜单
cout << "\n";
cout << "\t管理功能菜单\n";//与case顺序一致
cout << "\t 1. 图书信息管理 * \n ";
cout << "\t 2. 读者信息管理 * \n ";
cout << "\t 3. 返回上级操作 * \n ";

cout << "\t\n ";
cout << " ";
cout << "\t\n请选择您要执行的操作编号(1-3): ";
cout << endl;
}
void bMain() { //图书维护子菜单
cout << "\n";
cout << "\t图 书 信 息 维 护 管 理 员 后 台\n";//与case顺序一致
cout << "\t 1. 添加 * \n ";
cout << "\t 2. 修改 * \n ";
cout << "\t 3. 删除 * \n ";
cout << "\t 4. 查找 * \n ";
cout << "\t 5. 显示 * \n ";
cout << "\t 6. 返回上级操作 * \n ";
cout << "\t\n ";
cout << " ";
cout << "\t\n请选择您要执行的操作编号(1-6): ";
cout << endl;
}
void rMain() { //读者维护子菜单
cout << "\n";
cout << "\t读 者 信 息 维 护 管 理 员 后 台\n";//与case顺序一致
cout << "\t 1. 添加 * \n ";
cout << "\t 2. 修改 * \n ";
cout << "\t 3. 删除 * \n ";
cout << "\t 4. 查找 * \n ";
cout << "\t 5. 显示 * \n ";
cout << "\t 6. 返回上级操作 * \n ";
cout << "\t\n ";
cout << " ";
cout << "\t\n请选择您要执行的操作编号(1-6): ";
cout << endl;
}

void UI() //自定义函数实现颜色变化
{
system("color F0");//前景黑 背景亮白 白底黑字
}

main.cpp
include
using namespace std;

#include "interface.h"
#include "reader.h"

int main() {
char in = -1; //因为VS环境不能使用为初始化的变量,所以首先赋值为不可能使用的变量值以通过编译 char in=-1
login();
do {//退出系统限制条件
UI(); //系统界面改变
ygMain();
cin >> in;
switch (in) {
case'1':
yonghu();
break;
case'2':
guanli();
break;
case'0': {
cout << "感谢您使用本系统,再见!" << endl;
break;
}
default:
cout << "出现错误!请重试!" << endl;
}
} while (in != '0');
return 0;
}

reader.cpp
#include
#include
#include
using namespace std;

#include "reader.h"
#include "interface.h"

//读书管理模块
//读者类函数实现
void reader::addreader(int id, char na[]) {
bianhao = id;
strcpy_s(name, na);
cunzai = 1;
}
void reader::setjs(book bt) {
jsbook = bt;
}

rdata::rdata() {
reader rd;
top = -1;
fstream file("reader.txt", ios::in);
while (1) {
file.read((char*)&rd, sizeof(rd));
if (!file) break;
top++;
rjihe[top] = rd;
}
file.close();
}
rdata::~rdata() {
fstream file("reader.txt", ios::out);
for (int i = 0; i <= top; i++) {
if (rjihe[i].cunzai == 1)
file.write((char*)&rjihe[i], sizeof(rjihe[i]));
}
file.close();
}

void rdata::addreaders() {
reader rd;
int bianhao;
char rname[20];
cout << "请输入学号:";
cin >> bianhao;
cout << endl << "请输入姓名:";
cin >> rname;
cout << endl;
rd.addreader(bianhao, rname);
rd.jieshu = 2;
top++;
rjihe[top] = rd;
return;
}

int rdata::findreader(int suffix) {
int bianhao;
cout << "请输入学号:";
cin >> bianhao;
for (int i = 0; i <= top; i++) {
if (rjihe[i].bianhao == bianhao && rjihe[i].cunzai == 1) {
return i;
}
}
return -1;
}

int rdata::findreader() {
int bianhao;
char value[3];
cout << "请输入学号:";
cin >> bianhao;
for (int i = 0; i <= top; i++) {
if (rjihe[i].bianhao == bianhao && rjihe[i].cunzai == 1) {
if (rjihe[i].jieshu == 1)
strcpy_s(value, "借");
if (rjihe[i].jieshu == 2)
strcpy_s(value, "无");
cout << "学号:" << rjihe[i].bianhao << " " << "姓名:" << rjihe[i].name << " " << "是否借书:" << value << endl;
return i;
}
}
return -1;
}
void rdata::bj() {
int cur;
cur = findreader();
if (cur == 1) {
cout << "未找到该学生" << endl;
return;
}
cout << "请修改数据:" << endl;
cout << endl << "姓名:";
cin >> rjihe[cur].name;
return;
}

void rdata::sc() {
int cur;
cur = findreader();
if (cur == 1) {
return;
}
if (rjihe[cur].jieshu == 1) {
cout << "该用户已借出图书,请归还后删除!" << endl;
return;
}
rjihe[cur].cunzai = 2;
return;
}

void rdata::xs() {
char value[3];
for (int i = 0; i <= top; i++) {
if (rjihe[i].cunzai == 1) {
if (rjihe[i].jieshu == 1)
strcpy_s(value, "借");
if (rjihe[i].jieshu == 2)
strcpy_s(value, "无");
cout << "学号:" << rjihe[i].bianhao << " " << "姓名:" << rjihe[i].name << " " << "是否借书:" << value << endl;
}
}
return;
}

void rmanage() {
char in;
rdata bd;
do {
system("pause"); //暂停界面,以便显示所需数据
system("cls");
UI();
rMain();
cin >> in;
switch (in) {
case'1':
bd.addreaders();
break;
case'2':
bd.bj();
break;
case'3':
bd.sc();
break;
case'4':
bd.findreader();
break;
case'5':
bd.xs();
break;
case'6':
break;
default:
cout << "出现错误!请重试!" << endl;
}
} while (in != '6');
return;
}
void yonghu() {
char in;
do {
system("pause"); //暂停界面,以便显示所需数据
system("cls");
UI();
yMain();
cin >> in;
switch (in) {
case'1':
jiehuan(in);
break;
case'2':
jiehuan(in);
break;
case'3':
break;
default:
cout << "出现错误!请重试!" << endl;
}
} while (in != '3');
return;
}

把代码放一下

未初始化的局部变量,我猜。