nev*_*int 1 c++ parsing stringstream
我有一个看起来像这样的数据:
AAA 0.3 1.00 foo chr1,100
AAC 0.1 2.00 bar chr2,33
AAT 3.3 2.11 chr3,45
AAG 1.3 3.11 qux chr1,88
ACA 2.3 1.33 chr8,13
ACT 2.3 7.00 bux chr5,122
Run Code Online (Sandbox Code Playgroud)
请注意,上面的行是制表符分隔的.而且,它有时可能包含5个字段或4个字段.
我想要做的是将变量中的第4个字段捕获为"",如果它不包含任何值.
我有以下代码,但它以某种方式读取第5个字段,当第4个为空时为第4个字段.
这样做的正确方法是什么?
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int main ( int arg_count, char *arg_vec[] ) {
string line;
ifstream myfile (arg_vec[1]);
if (myfile.is_open())
{
while (getline(myfile,line) )
{
stringstream ss(line);
string Tag;
double Val1;
double Val2;
double Field4;
double Field5;
ss >> Tag >> Val1 >> Val2 >> Field4 >> Field5;
cout << Field4 << endl;
//cout << Tag << "," << Val1 << "," << Val2 << "," << Field4 << "," << Field5 << endl;
}
myfile.close();
}
else { cout << "Unable to open file"; }
return 0;
}
Run Code Online (Sandbox Code Playgroud)