-1

我从带有头部和尾部的传感器发送 6 个数据,数据之间带有 (,)

$data0,data1,data2,data3,data4,data5%.

我想解析/排序这些数据并成为:

y0=data0
y1=data1...

怎么做?我使用 Visual Studio C++ 2010。

我的程序是:

#include <iostream>
#include <string>
#include <sstream>
#include "stdafx.h"
using namespace std;

int main()
  {
string str = "$data1,data2,data3,data4,data5,data6%";
string word;
stringstream stream(str);
while( getline(stream, word, ',') )
cout << word << "\n";
 }

价值是

$data1
data2
data3
data4
data5
data6%

如何擦除头部和尾部,以及如何使缓冲区中的数据

Y1=data1
y2=data2
 ...
4

1 回答 1

0

This code shows how it can be done:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() {
    std::string s("$data0,data1,data2,data3,data4,data5%");

    s.erase(0, 1); // Remove header.
    s.erase(s.size() - 1, 1); // Remove tail.

    // Parse tokens.
    std::vector<std::string> tokens; // Vector will store tokens.
    std::istringstream iss(s);
    for (std::string token; std::getline(iss, token, ',');) {
        tokens.push_back(token);
    }

    // Output tokens.
    for (const auto& t : tokens) {
        std::cout << t << std::endl;
    }
}

Output:

data0
data1
data2
data3
data4
data5
于 2013-03-21T00:39:29.937 回答