2012-07-26 41 views
1

賬戶號碼種類金額搜索特定行C++標籤delmited

15檢查52.42

23節約51.51

11檢查12.21

是我的標籤delmited文件

我希望能夠通過t搜索行他的賬號。說如果我放在23,我想獲得特定的行。 id如何做到這一點?

也更先進,如果我想改變一個具體的價值,比如在帳戶23中說51.51。我如何獲取該值並將其替換爲新值?

到目前爲止IM按行

串線行只是閱讀; ifstream is(「account.txt」); ifstream is(「account.txt」);

if (is.is_open()) 
{ 
while (std::getline(is, line)) // read one line at a time 
{ 
    string value; 
    string parseline; 
    std::istringstream iss(line); 

    getline(line, parseline); 
    cout << parseline << endl; // do something with the value 
    while (iss >> value) // read one value at at time from the line 
    { 
     //cout << line << " "; // do something with the value 
    } 
} 
is.close(); 
} 
else 
    cout << "File cant be opened" << endl; 

return 0; 
+1

如果只有某種方式可以[關係](http://en.wikipedia.org/wiki/Relational_database)方式[查詢](http://www.sqlite.org)數據... – smocking 2012-07-26 18:42:00

+0

@smocking就像你的用戶名符合你的迴應。 – 2012-07-26 18:53:59

回答

2

鑑於每行的長度是可變的,沒有辦法索引特定行,而不首先解析整個文件。

但我懷疑你的程序會想要操縱隨機的行和列。所以我首先解析出整個文件。將每行放入數組中的自己的數據結構中,然後將該行索引到數組中。

您可以使用「strtok」將輸入拆分爲多行,然後再次將strtok拆分成字段。

0

如果我要這樣做,我會先編寫一些解析整個文件並將數據存儲在適當數據結構(如數組或std :: map)中的函數。然後我將使用數據結構進行所需的操作(例如搜索或編輯)。最後,如果有任何修改,我會將數據結構寫回文件。