读取整数对,直到文本输入文件中的换行符
Reading pairs of integers until newline in text input file
嘿伙计们,所以我有一个问题。我想读取整数对(第一个是系数,第二个是指数),这些对中的每一个都是链表中的一个节点。我会继续用这些对填充链表,直到它在输入文本文件中看到换行符或 Enter 键。
下一行它将再次开始,因此输入文件将如下所示
-1 0 6 2 3 2 5 6 1 6
2 5 3 2 4 2 5 7 2 7
阅读后它将是两个不同的多项式,即
多项式 1 =
-1 + 6x^2 + 3x^2 + 5x^6 + x^6
多项式 2 =
2x^5 + 3x^2 + 4x^2 + 5x^7 + 2x^7
或 2 个不同的链表,每个多项式一个。因为目前我的方式,如果我只是使用喜欢
while (infile >> coefficient >> exponent)
{
polynomialA.listInsert(coefficient, exponent);
}
它将读取两行并创建一个很长的单个多项式。
编辑:对不起,我想我不清楚。问题是 - 如何使 ifstream 继续读取 intgers 对,直到它到达文本文件中的换行符。
您可以将其分成几行,然后是标记: 提示:将std::stringstream
与std::getline
一起使用
std::string line;
std::getline(infile, line) //Read the whole line
{
std::stringstream ss(line);
while(ss >> coefficient >> exponent) //read the pairs
polynomialA.listInsert(coefficient, exponent);
}
std::getline(infile, line) //read another whole line
{
std::stringstream ss(line);
while(ss >> coefficient >> exponent) //read the pairs
polynomialB.listInsert(coefficient, exponent);
}
当然,以上可以用更好的方式写,但是,我会把它留给你。
相关文章:
- C++文本文件输入
- C++ 中文件输入输出中的长长 v/s int
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 用空格填充 2D 数组(从文件输入)
- 流对象来表示文件输入,然后是标准输入?
- 用精神解析文件输入
- 从文件输入和输出
- 来自文本文件输入 c++ 的向量
- 如何通过键盘或分数文件输入分数
- 了解文件输入和输出,我可以写入文件,但无法从文件打印
- 词频程序 - 文件输入太大?
- 尝试从 csv 文件输入大量数据时出错
- 如何将文件从文件输入到C 中的矩阵中
- 如何从Windows CMD运行可执行文件并将其传递给文件输入的参数参数和重定向
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- 这是读取文件输入的正确方法吗?
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 如何创建一个具有空值、递减编号系统和.txt文件输入值的二维阵列网格
- C 文件输入流循环问题
- 使用对输入整数的文件输入