2009-07-23 29 views
0

我的測試文件中有數據是這樣的:C++從文本文件顯示幫助輸入

1 
2 
3 
0 
1, 2 
3, 4 
0, 0 
4, 3 
2, 1 
0, 0 

我怎麼會通過線路的數據分開,但分開也由零數據的每個部分。

ifstream data("testData.txt"); 
string line, a, b; 


while(getline(data,line)) 
{ 
    stringstream str(line); 
    istringstream ins; 
    ins.str(line); 
    ins >> a >> b; 

    hold.push_back(a); 
    hold.push_back(b); 
} 

如何將它們分隔爲零?

+0

做些什麼......?做什麼?你的預期產出是多少? – Extrakun 2009-07-23 03:27:46

回答

1

所以這些行很重要,並且零分隔的數字列表也很重要?嘗試這樣的:

std::ifstream data("testData.txt"); 
std::vector<int> hold; 
std::string line; 
std::vector<std::string> lines; 

while(std::getline(data,line)) 
{ 
    lines.push_back(line); 
    std::stringstream str(line); 

    // Read an int and the next character as long as there is one 
    while (str.good()) 
    { 
    int val; 
    char c; 
    str >> val >> c; 
    if (val == 0) 
    { 
     do_something(hold); 
     hold.clear(); 
    } 
    else 
     hold.push_back(val); 
    } 
} 

這不是很容錯,但它的工作原理。它依靠一個字符(逗號)出現在除了每行最後一個數字之外的每個數字之後。

3

首先,我會盡力改善問題定義☺

0

當你完成你

[1,2,3,0,1,2,3,4,0,0,4,3,2,1,0,0]

如何使用std :: find()方法?