次のようなデータがあります。
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 つのフィールドを含む場合もあります。
私がやりたいことは、値が含まれていない場合、変数の4番目のフィールドを "" としてキャプチャすることです。
私は次のコードを持っていますが、どういうわけか、4番目が空のときに4番目のフィールドとして5番目のフィールドを読み取ります。
それを行う正しい方法は何ですか?
#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;
}