-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfoFile.cpp
136 lines (105 loc) · 2.47 KB
/
InfoFile.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "stdafx.h"
#include "InfoFile.h"
CInfoFile::CInfoFile()
{
}
CInfoFile::~CInfoFile()
{
}
//读取登陆信息
void CInfoFile::ReadLogin(CString &name, CString &pwd)
{
ifstream ifs; //创建文件输入对象
ifs.open(_F_LOGIN); //打开文件
char buf[1024] = { 0 };
ifs.getline(buf, sizeof(buf)); //读取一行内容
name = CString(buf); //char *转换为CString
ifs.getline(buf, sizeof(buf));
pwd = CString(buf);
ifs.close(); //关闭文件
}
//修改密码
void CInfoFile::WritePwd(char* name, char* pwd)
{
ofstream ofs; //创建文件输出对象
ofs.open(_F_LOGIN); //打开文件
ofs << name << endl; //name写入文件
ofs << pwd << endl; //pwd写入文件
ofs.close(); //关闭文件
}
//读取商品信息
void CInfoFile::ReadDocline()
{
ifstream ifs(_F_STOCK); //输入方式打开文件
char buf[1024] = { 0 };
num = 0; //初始化商品数目为0
//取出表头
ifs.getline(buf, sizeof(buf));
while (!ifs.eof()) //没到文件结尾
{
msg tmp;
ifs.getline(buf, sizeof(buf)); //读取一行
num++; //商品数目加一
//AfxMessageBox(CString(buf));
char *sst = strtok(buf, "|"); //以“|”切割
if (sst != NULL)
{
tmp.id = atoi(sst); //商品id
}
else
{
break;
}
sst = strtok(NULL, "|");
tmp.name = sst; //商品名称
sst = strtok(NULL, "|");
tmp.price = atoi(sst); //商品价格
sst = strtok(NULL, "|");
tmp.num = atoi(sst); //商品数目
sst = strtok(NULL, "|");
tmp.supplier = sst; //供应商
ls.push_back(tmp); //放在链表的后面
}
ifs.close(); //关闭文件
}
//商品写入文件
void CInfoFile::WirteDocline()
{
ofstream ofs(_F_STOCK);//输出方式打开文件
if (ls.size() > 0) //商品链表有内容才执行
{
ofs << "商品ID|商品名称|商品价格|库存|供应商" << endl; //写入表头
//通过迭代器取出链表内容,写入文件,以“|”分隔,结尾加换行
for (list<msg>::iterator it = ls.begin(); it != ls.end(); it++)
{
ofs << it->id << "|";
ofs << it->name << "|";
ofs << it->price << "|";
ofs << it->num << "|";
ofs << it->supplier << endl;
}
}
ofs.close();//关闭文件
}
//添加新商品
//name:商品名称,num:库存,price:价格
void CInfoFile::Addline(CString name, int num, int price, CString supplier)
{
msg tmp;
if (ls.size() > 0)
{
//商品名称,库存,价格有效
if (!name.IsEmpty() && num > 0 && price > 0)
{
tmp.id = ls.size() + 1; //id自动加1
CStringA str;
str = name; //CString转CStirngA
tmp.name = str.GetBuffer(); //CStirngA转char *,商品名称
tmp.num = num; //库存
tmp.price = price; //价格
str = supplier;
tmp.supplier = str.GetBuffer();
ls.push_back(tmp); //放在链表的后面
}
}
}