Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ stringstream未按预期工作_C++_Vector_Stringstream - Fatal编程技术网

C++ stringstream未按预期工作

C++ stringstream未按预期工作,c++,vector,stringstream,C++,Vector,Stringstream,我正在尝试编写程序来解析和处理文本文件。 在未能成功实现sscanf后,我决定尝试stringstream 我有字符串向量,包含由空格分隔的数据,如: 一些字符串另一个字符串还有另一个字符串变量 下一串 我编写了代码,预期结果是: Counter: 4 Variable number 1 : VARIABLE_STRING_NO_1 Variable number 2 : VARIABLE_STRING_NO_2 Variable number 3 : VARIABLE_STRING_NO_3

我正在尝试编写程序来解析和处理文本文件。 在未能成功实现sscanf后,我决定尝试stringstream

我有字符串向量,包含由空格分隔的数据,如:

一些字符串另一个字符串还有另一个字符串变量 下一串

我编写了代码,预期结果是:

Counter: 4
Variable number 1 : VARIABLE_STRING_NO_1
Variable number 2 : VARIABLE_STRING_NO_2
Variable number 3 : VARIABLE_STRING_NO_3
Variable number 4 : VARIABLE_STRING_NO_4
但我得到的却是:

Counter: 4
Variable number 1 : VARIABLE_STRING_NO_1
Variable number 2 : VARIABLE_STRING_NO_1
Variable number 3 : VARIABLE_STRING_NO_1
Variable number 4 : VARIABLE_STRING_NO_1
有人能把我推到正确的方向吗?(例如,使用其他容器代替矢量,将方法更改为…等)

如果变量\u STRING包含两个子字符串,中间有空格,该怎么办?这在我的数据中是可能的

示例代码:

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
    vector<string> vectorOfLines, vectorOfData;

    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_1 next_string");
    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_2 next_string");
    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_3 next_string");
    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_4 next_string");

    string data = "", trash = "";
    stringstream token;

    int counter = 0;

    for( int i = 0; i < (int)vectorOfLines.size(); i++ )
    {
        token << vectorOfLines.at(i);
        token >> trash >> trash >> trash >> data >> trash;
        vectorOfData.push_back(data);                       //  wrong method here?
        counter++;                                          //  counter to test if for iterates expected times
    }

    cout << "Counter: " << counter << endl;

    for( int i = 0; i < (int)vectorOfData.size(); i++ )
    {
        cout << "Variable number " << i + 1 << " : " << vectorOfData.at(i) << endl;
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
矢量线,矢量数据;
线的向量。推回(“一些字符串另一个字符串还有另一个字符串变量下一个字符串”);
线的向量。向后推(“一些字符串另一个字符串还有另一个字符串变量下一个字符串”);
线的向量。向后推(“一些字符串另一个字符串还有另一个字符串变量下一个字符串”);
线的向量。推回(“一些字符串另一个字符串还有另一个字符串变量下一个字符串”);
字符串数据=”,垃圾=”;
串流令牌;
int计数器=0;
对于(int i=0;i<(int)vectorOfLines.size();i++)
{
令牌>垃圾>>垃圾>>垃圾>>数据>>垃圾;
vectorOfData.push_back(data);//这里的方法错误吗?
计数器+++;//用于测试是否迭代预期时间的计数器
}

无法您想在读取单个字符串后重置字符串流。从外观上看,您正在使用的字符串流进入失败状态。此时,在状态变为
clear()之前,它将不会删除任何进一步的输入
。此外,您应该始终验证您的阅读是否成功。也就是说,我将以如下方式启动循环体:

token.clear();
token.str(vectorOfLines[i]);
if (token >> trash >> trash >> trash >> data >> trash) {
    process(data);
}
else {
    std::cerr << "failed to read '" << vectorOfLines[i] << "\n";
}
token.clear();
str(向量线[i]);
如果(令牌>>垃圾>>垃圾>>垃圾>>垃圾>>数据>>垃圾){
过程(数据);
}
否则{

std::cerr您希望在读取单个字符串后重置字符串流。从外观上看,您正在使用的字符串流进入失败状态。此时,在状态变为
clear()之前,它将不会删除任何进一步的输入
。此外,您应该始终验证您的阅读是否成功。也就是说,我将以如下方式启动循环体:

token.clear();
token.str(vectorOfLines[i]);
if (token >> trash >> trash >> trash >> data >> trash) {
    process(data);
}
else {
    std::cerr << "failed to read '" << vectorOfLines[i] << "\n";
}
token.clear();
str(向量线[i]);
如果(令牌>>垃圾>>垃圾>>垃圾>>垃圾>>数据>>垃圾){
过程(数据);
}
否则{

std::cerr
令牌>>垃圾>>垃圾>>垃圾>>数据>>垃圾;
的目标是什么?顺便说一下,在插入
令牌之前,打印
矢量线。在(i)
生成正确的结果并打印
令牌.str()
插入行后,总是会产生第一行。您需要检查>>运算符是否成功,否则您使用的是上一次迭代中的字符串旧值。@BostonJohn trash读取的字符串不需要输出,只有数据才重要,因此会覆盖trash。
标记>>tr的目标是什么ash>>垃圾>>垃圾>>数据>>垃圾;
?顺便说一句,在插入
标记之前,打印
矢量线。在(i)
处,会生成正确的结果并打印
标记.str()
插入行后,总是会产生第一行。您需要检查>>运算符是否成功,否则您使用的是上一次迭代中字符串的旧值。@BostonJohn trash读取的字符串不需要用于输出,只有数据才重要,因此trash会被覆盖Hanks很多,现在我可以用C“玩”了+再也没有什么能像击打墙一样。爱这个有帮助的社区!谢谢,现在我可以再玩“C++”了。