在多向量中读取文件和存储数据
C++ : Reading file and store data in multi-vector
我正在尝试用这种格式读取数据文件
T1: I1,I2,I5
T2: I2,I4
T3: I2,I3
T4: I1,I2,I4
T5: I1,I3
T6: I2,I3
T7: I1,I3
T8: I1,I2,I3,I5
T9: I1,I2,I3
我不想读第一列T1,T2,T3 ......,但是每行都将是一个数据集,我想在(空格' ')之后开始读取并以每行结束,以及我如何根据(逗号',')分隔数据
我写了这段代码,但它没有正常工作,它读取第一列
string CItem;
// set of elements
set< CItem > CItemSet;
//Transactions
CItemSet CTransaction;
// set of transactions
vector< CTransaction > CTransactionSet;
ifstream inFile(inFileName);
if (!inFile)
{
cout << "Failed to open input file filename:." << inFileName;
}
CTransactionSet transSet;
CTransaction tran;
string txtLine;
// read every line from the stream
while (getline(inFile, txtLine))
{
istringstream txtStream(txtLine);
string txtElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while (getline(txtStream, txtElement, ','))
{
if (txtElement == ": ") break;
else tran.insert(txtElement);
}
transSet.push_back(tran);
}
既然你有
CTransaction tran;
在第一个while
循环之外,项目继续被添加到它。把它移到while
循环中
CTransactionSet transSet;
string txtLine;
// read every line from the stream
while (getline(inFile, txtLine))
{
CTransaction tran;
我通过做你说的R Sahu来解决这个问题,但最重要的解决方案是使用。ignore,这样我们就不会读取' '之前的部分
CTransactionSet transSet;
string txtLine;
// read every line from the stream
while (getline(inFile, txtLine))
{
istringstream txtStream(txtLine);
txtStream.ignore(txtLine.length(), ' ');
// read every element from the line that is seperated by commas
// and put it into the vector or strings
string txtElement;
CTransaction tran;
while (getline(txtStream, txtElement, ','))
{
tran.insert(txtElement);
}
transSet.push_back(tran);
}
相关文章:
- 使用 char* 存储数据和产生的错误
- 我可以存储数据的地方/C++,Linux,QtCreator
- 如何在共享库的整个生命周期内存储数据
- 在字符串数组中动态创建和存储数据
- 用指向文件的指针存储数据结构
- 如何存储数据/我们如何获取存储在 unordered_map c++11 中存储桶中的数据
- 存储数据的对象和存储指向数据的智能指针的对象
- C++读取文本文件并存储数据
- Cocos2dx: 无法存储数据?
- 如何在每次循环迭代期间生成向量,存储数据,然后删除该向量?
- 尝试从每个预期数据之间有多个空间的文件中读取和存储数据
- 存储数据列表并自动分配给结构的最佳方法是什么
- QT MAC应用程序存储数据路径
- 动态分配存储数据在堆中的随机位置中
- 鸟类调查中的项目在代码中遇到问题.如何像数据库系统一样存储数据
- 可以在C 中永久存储数据中的数据
- 用于存储数据的结构和外部二进制文件
- 使用 char 数组存储数据
- 在结构中读取和存储数据
- 尝试在结构中存储数据时出现堆分配错误