我有一个关于如何使用 isstream 从我的输入 .dat 文件中读取数据以用作和弦实现的问题我有各种长度和类型的输入。我的输入示例如下所示:
addC 2 #ignore this msg
addC 3 #ignore this msg
addC 9 #ignore this msg
print 9
insert 2 data0
insert 9 data1
insert 3 data2
我想逐行捕获每个输入,忽略 # 键后面的任何消息。
我到目前为止所做的如下:
if (myfile.is_open())
{
while (getline(myfile, line))
{
string command;
string id;
string input;
unsigned int hashKey = 0;
istringstream iss(line);
getline(iss, command, ' ');
getline(iss, number, ' ');
getline(iss, message, '#');
//other codes
}
}
但是,当我读入我的文件时,它似乎有时会以某种方式读入超过所需的内容。例如,
//插入 0 之前的前几条命令很好
command = insert number = 2 input = data0 INSERTING
data0 at: 2
command = insert number = 9 input = data1
command = insert number = 3 input = data2
//程序在这里停止,因为我相信阅读中有一些错误