33,311
社区成员
发帖
与我相关
我的任务
分享
ifstream one;
.....省略
one >> temp; //temp是int类型
while (temp != id) //读取到ID符合那行为止
{
while (one.get(q) && q != ','); //读取到逗号为止——读取有效数据之前的代码,q是char类型
one >> temp;
cout << "," << temp << "," << id << endl;
}
stringstream oss;
// 卡牌读取
oss << line; // 转换成流
oss >> id >> name >> pz >> jc[0] >> jc[1] >> jc[2] ; // 读取一行流
// 测试
cout << id << name << pz << jc[0] << jc[1] << jc[2] << endl;
这里改下,刚才忘记读取 pz了#include <iostream> // std::cout
#include <string>
#include <fstream> // std::ifstream
#include <sstream> // std::ifstream
using namespace std;
int main()
{
ifstream infile("card.TXT");
string line ;
//顺序为:ID 名字 品质 基础生命系数 基础攻击系数 基础防御系数
int id;
string name;
int pz;
double jc[3];
while (getline(infile, line)) {
// 排除空行 和 注释行
if (line.size() < 3 || line[0] == '/')
continue;
// 品质行读取
if (line[0] == ',') {
cout << "品质行" << line << endl;
continue;
}
stringstream oss;
// 卡牌读取
oss << line; // 转换成流
oss >> id >> name >> jc[0] >> jc[1] >> jc[2] ; // 读取一行流
// 测试
cout << id << name << jc[0] << jc[1] << jc[2] << endl;
}
return 0;
}
/* 输出结果
品质行,4 3 3 2 1 1 , //q用来表示终止符
0宅男122
1普通人12.21.9
2工人12.51.7
3学生11.82.2
100黑市拳手232.5
101红棍打手22.82.4
102百年僵尸23.22.6
200少年东伯雪鹰343
201隐杀家明333.5
202永夜君王千夜33.43.3
300战神级强者458
301一人破军47.58.3
400行者孙5914
500半步基因锁五阶61322
*/
这样测试可以正确读取了,不需要逗号 using namespace std;
if (i == 0)
{
srand(unsigned int(time(0)));
int ra = rand() % 10000;
if (ra < 3000)quality = 1; //白色30%
else if (ra < 6000)quality = 2; //绿色30%
else if (ra < 8000)quality = 3; //蓝色20%
else if (ra < 9500)quality = 4; //紫色15%
else if (ra < 9980)quality = 5; //橙色4.8%
else quality = 6; //神话0.2%
//决定抽卡的品质
}
else quality = i;
ifstream one;
one.open("card.txt");
if (!one.is_open())
{
cout << "card.txt文件(用于存储卡牌信息)损坏" << endl;
system("pause");
}
char q;
while (one.get(q) && q != ','); //读取到逗号为止——读取有效数据之前的代码
int temp = 0;
for (int i = 0;i < quality;i++)
one >> temp; //读取当前品质卡的数量
string tem;
getline(one, tem); //读取掉第一行全部
id = (quality - 1) * 100 + rand() % temp; //决定卡的ID,注意,每个卡片数量不能低于1,必须要有卡
one >> temp;
while (temp != id) //读取到ID符合那行为止
{
getline(one, tem);
one >> temp;
cout << tem << "," << temp << "," << id << endl;
system("pause");
}
cout << "抽取中";
cout << endl;
one >> name >> quality >> base_hp >> base_atk >> base_def; //读取五个属性
显示:
文件内容为:
所以很奇怪的说#include <iostream> // std::cout
#include <string>
#include <fstream> // std::ifstream
#include <sstream> // std::ifstream
using namespace std;
int main()
{
ifstream infile("card.TXT");
string line ;
stringstream oss;
int id;
while (getline(infile, line)) {
// 排除空行 和 注释行
if (line.size() < 3 || line[0] == '/')
continue;
cout << line << endl;
}
return 0;
}
/* 输出结果
4 3 3 2 1 1 , //,用来表示终止符
0 宅男 1 2.0 2.0 1.0,
1 普通人 1 2.2 1.9 0.9,
*/