2012-02-03 63 views
1

我的數據文件的第一行是數據的三個(或四個)欄標題:函數getline()函數蕉標題一起

%b02_a08 b02_a08_counts b02_a08_eu 

正如你所看到的,列製表符分隔。當我使用getline()時,它將整條線路拼湊在一起。如果我使用getline(,,'\t'),則會爲該行返回%b02_a08。我需要把這一行分開,因爲我讀了每個標題。

如果有人知道如何從數據文件中讀取頭文件並將它們放入vector<string>以便我可以在其上運行Regex以找到其中包含「eu」的文件,那就是我所需要做的。那麼,讀取數據列,我可能會遇到同樣的TAB分隔問題。

+0

(猜測語言是'C++'的矢量引用,請不要忘記下次添加語言標記,並糾正這一個,如果我弄錯了。 ) – Mat 2012-02-03 13:52:51

回答

0

使用流路......

中,

// process the header 
std::getline(some_input_stream, line)) 
{ 
    std::istringstream str(line); 
    // Now reaad each field 
    str >> header_1 >> header_2 >> header_3; 
    if (str >> header_4) 
    { 
    // process the optional column 
    have_col_4 = true; 
    } 
} 

// Now to read the rows 
while(some_input_stream >> col_1 >> col_2 >> col_3) 
{ 
    if (have_col_4) 
    some_input_stream >> col_4; 
} 

最後一個塊可以std::getline做太(即讀取一行,構建一個流,然後從流中讀取) ,但這往往會稍微慢一點...

+0

我將如何使用push_back()來放置str(或「liness」,whic h是我的)將標題放入一個向量中?: 'getline(datFile,line); istringstream liness(line); liness >> 而 { \t函數getline(liness,溫度, '\ T')(liness.end!); \t header.push_back(temp); }' – user1187621 2012-02-03 14:11:48

+0

您可以使用@ dasblinkenlight的答案中時髦的'copy'行或者簡單的while循環('while(liness >> header_item){vec.push_back(header_item);}' - 記住流會使用空格作爲字符串的分隔符) – Nim 2012-02-03 14:15:41

+0

header_item是我需要聲明的字符串? – user1187621 2012-02-03 14:20:41