我有一个看起来像这样的数据:
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
请注意,上面的行是制表符分隔的。此外,它有时可能包含 5 个字段或 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;
}