来自 istringstream 的五倍输出
Fivefold output from istringstream
我想在检测到某个子字符串后输出每一行。不知何故,我得到了iss
五倍的输出.这是我的代码:
//load all data from txt
string data;
std::ifstream infile("SavedData.txt");
while (std::getline(infile, data)) {
std::istringstream iss(data);
string d;
while (iss >> d) {
//extract substring
unsigned firstBracket = data.find("(");
unsigned lastBracket = data.find(")");
string coordSynth = data.substr(firstBracket + 1, lastBracket - firstBracket - 1);
cout << coordSynth << endl;
}
}
现在的输出是这样的:
0.0, 45.0, -390.0
0.0, 45.0, -390.0
0.0, 45.0, -390.0
0.0, 45.0, -390.0
0.0, 45.0, -390.0
0.0, 45.0, -314.3
0.0, 45.0, -314.3
0.0, 45.0, -314.3
0.0, 45.0, -314.3
0.0, 45.0, -314.3
etc.
其实我只是想要
0.0, 45.0, -390.0
0.0, 45.0, -314.3
0.0, 45.0, -277.3
etc.
不,在 txt 文件中没有重复。此文件如下所示:
0001(0.0, 45.0, -390.0).png
0003(0.0, 45.0, -314.3).png
0007(0.0, 45.0, -277.3).png (and so on...)
你在这里的问题是
unsigned firstBracket = data.find("(");
unsigned lastBracket = data.find(")");
string coordSynth = data.substr(firstBracket + 1, lastBracket - firstBracket - 1);
cout << coordSynth << endl;
这是从0001(0.0, 45.0, -390.0).png
中获取0.0, 45.0, -390.0
的逻辑,它位于一个你甚至没有做任何事情的while循环中。 该循环将为每行输入执行 5 次(因为有五个"子字符串"(,因此您将获得 5 个输出。 您需要做的就是摆脱 while 循环,因为您没有对行中包含的单个字符串执行任何操作。 这给了你类似的东西
int main()
{
std::string data;
std::istringstream infile("0001(0.0, 45.0, -390.0).pngn0003(0.0, 45.0, -314.3).pngn0007(0.0, 45.0, -277.3).pngn");
while (std::getline(infile, data)) {
std::istringstream iss(data);
std::string d;
//extract substring
unsigned firstBracket = data.find("(");
unsigned lastBracket = data.find(")");
std::string coordSynth = data.substr(firstBracket + 1, lastBracket - firstBracket - 1);
std::cout << coordSynth << std::endl;
}
}
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 来自 istringstream 的五倍输出
- C++随机函数给出的相同整数是输出的两倍
- 程序编译并执行,但输出的不合理大的数字作为最大值.如何显示长双倍?
- C++初级读本第五版字符串练习,要求两段不同的代码提供相同的输出
- 将 2s 赞美转换为小数.输出是双倍的
- ReadFile, CreateProcess, Pipe标准输出重定向,双倍输出
- 如何定义使用五倍相同参数类型的函数类型