字符串流提取不起作用

发布于 2024-07-29 00:14:43 字数 1329 浏览 7 评论 0原文

我似乎在从字符串流中提取数据时遇到问题。 我的提取开始似乎缺少前两个字符。

我有类似于以下代码的内容:

    std::stringstream ss(
    std::stringstream::in |
    std::stringstream::out
    );

    bool bValid;
    double dValue;
    double dTime;

    for( (int i = 0; i < 5; i++ )
    {
        bValid = getValid();
        dValue = getValue();
        dTime = getTime();

        // add data to stream
        ss << bValid;
        ss << dValue;
        ss << dTime;
    }

    int strsize = ss.str().size();
    char* data = new char[strsize];
    std::strcpy(data, ss.str().c_str());

    // then do stuff with data
    // ...... store data in an Xml Node as CDATA

    // read data back
    std::stringstream ssnew( std::stringstream in | std::stringstream out );
    ss.clear();
    ss << getCharData(); // returns a char* and puts it in stream.

    for( int i = 0; i < 5; i++ )
    {
        ssnew >> bValid;  // do something with bValid
        ssnew >> dValue;  // do something with dValue
        ssnew >> dTime;   // do something with dTime
    }

我遇到一个问题,当我在从“ssnew”读取数据时使用提取运算符时,它似乎会跳过前两个字符。 例如,在调试器中,它显示字符串流具有“001.111.62.2003...等”。 然而,在第一个“ssnew >> bValid”之后,bValid 变为“true”,dValue 变为“0.111”,dTime 变为“0.62”,表明流中的前两个零被忽略。 为什么它不从流的开头开始?

干杯, 赛斯

I seem to be having a problem with extracting data from a stringstream. The start of my extraction seems to be missing the first two characters.

I have something similar to the following code:

    std::stringstream ss(
    std::stringstream::in |
    std::stringstream::out
    );

    bool bValid;
    double dValue;
    double dTime;

    for( (int i = 0; i < 5; i++ )
    {
        bValid = getValid();
        dValue = getValue();
        dTime = getTime();

        // add data to stream
        ss << bValid;
        ss << dValue;
        ss << dTime;
    }

    int strsize = ss.str().size();
    char* data = new char[strsize];
    std::strcpy(data, ss.str().c_str());

    // then do stuff with data
    // ...... store data in an Xml Node as CDATA

    // read data back
    std::stringstream ssnew( std::stringstream in | std::stringstream out );
    ss.clear();
    ss << getCharData(); // returns a char* and puts it in stream.

    for( int i = 0; i < 5; i++ )
    {
        ssnew >> bValid;  // do something with bValid
        ssnew >> dValue;  // do something with dValue
        ssnew >> dTime;   // do something with dTime
    }

I am having a problem that when I use the extraction operator when reading data from "ssnew" it seems to skip the first two characters. In the debugger, for example, it is showing that the stringstream has "001.111.62.2003... etc". However, after the first "ssnew >> bValid" bValid becomes "true" and dValue becomes "0.111" and dTime becomes "0.62" indicating that the first two zeros in the stream are being ignored. Why isn't it starting at the beginning of the stream?

Cheers,
Seth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耶耶耶 2024-08-05 00:14:43

尝试:

    // add data to stream
    ss << bValid << " ";
    ss << dValue << " ";
    ss << dTime << " ";

Try:

    // add data to stream
    ss << bValid << " ";
    ss << dValue << " ";
    ss << dTime << " ";
女皇必胜 2024-08-05 00:14:43

您的原始代码不起作用的原因是提取是贪婪的,因此 ssnew >>> bValid 吞噬了“001”。

请注意,strstream 已被弃用,取而代之的是 stringstream。

The reason your original code didn't work is that the extraction was greedy, so ssnew >> bValid gobbled up the "001".

Note that strstream is deprecated in favor of stringstream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文