C ++ getline()在第二次迭代中没有得到分隔符

时间:2017-06-15 13:02:04

标签: c++ delimiter getline

所以,我有一些看起来像这样的数据表:

"1444394028","1","5339","M","873"
"1444394028","1","7045","V","0.34902"
"1444394028","1","7042","M","2"
"1444394028","1","7077","V","0.0470588"

我正在使用此方法来分离数据:

int i = 0;

std::ifstream dataInput(path);
std::string line2;

char delimeter(',');

std::string timestampTemp, millisecondsSinceStartTemp, deviceTemp, typeTemp, valueTemp;

while (std::getline(dataInput, timestampTemp, delimeter)) {
    while (i < 3) {

        std::vector<struct struct_t> writedata(1);


        std::getline(dataInput, millisecondsSinceStartTemp, delimeter);
        std::getline(dataInput, deviceTemp, delimeter);
        std::getline(dataInput, typeTemp, delimeter);
        std::getline(dataInput, valueTemp);

        std::cout << "TEST " << timestampTemp << " ...." << millisecondsSinceStartTemp << " ....." << deviceTemp << "..... " << typeTemp << "...... " << valueTemp << endl;

        writedata[0].timestamp = atoi(timestampTemp.substr(1, timestampTemp.size() - 2).c_str());
        std::cout << "!" << endl;
        writedata[0].millisecondsSinceStart = atoi(millisecondsSinceStartTemp.substr(1, millisecondsSinceStartTemp.size() - 2).c_str());
        std::cout << "!" << endl;
        writedata[0].device = atoi(deviceTemp.substr(1, deviceTemp.size() - 2).c_str());
        std::cout << "!" << endl;
        writedata[0].value = atof(valueTemp.substr(1, valueTemp.size() - 2).c_str());
        std::cout << "!" << endl;


        writedata[0].type = (typeTemp.substr(1, typeTemp.size() - 2).c_str());

        std::cout << "!" << endl;
        std::cout << "-data to be written: " << writedata[0].timestamp << " - " << writedata[0].millisecondsSinceStart << " - " << writedata[0].device << " - " << writedata[0].value << " - " << writedata[0].type << endl;


        this->append(writedata.data(), &i);

        timestampTemp.clear();
        millisecondsSinceStartTemp.clear();
        deviceTemp.clear();
        typeTemp.clear();
        valueTemp.clear();

        i++;
    }
}

它会创建此输出:

TEST "1444394028" ...."1" ....."5339"..... "M"...... "873"
!
!
!
!
! 
-data to be written: 1444394028 - 1 - 5339 - 873 - M
TEST  ...."1444394028" ....."1"..... "7045"...... "V","0.34902"

正如您所看到的,第二次迭代不起作用,我不能说为什么。我已经有了它,但看起来我已经毁了它。与其他解决方案不同,文件中也没有空格。一如既往,感谢您的帮助。

0 个答案:

没有答案