2016-11-11 30 views
1

我試圖在C++中創建一個字典,但是在'='分隔符上分割文本文件時遇到問題。理想情況下,它會在兩個陣列中。我想將行的'='的左側放入數組[0],將右側放入數組[1],然後使用數組[0]作爲鍵並將數組[1]用作預製的值插入功能,例如。 dictionary.insert(array [0],array [1])。我已經建立了字典邏輯,但是在分割線路時遇到了麻煩。將'='分隔線上的文本文件逐行分解C++

下面是未使用等號作爲分隔符,並且因此把「=」到數組[1]我的(可怕)代碼:

int main() { 

    Dictionary englishToEsperanto; 

    ifstream infile("Dictionary.txt"); 

    string line; 
    string arr[2]; 

    if (infile.is_open()) 
    { 
     while (getline(infile, line)) 
     { 
      int i = 0; 
      stringstream ssin(line); 
      while (ssin.good() && i < 2) { 
       ssin >> arr[i]; 
       ++i; 
      } 
      for (i = 0; i < 2; i++) { 
       cout << arr[i] << ' '; 
      } 
       cout << endl; 
     } 

     infile.close(); 
    } 
    else 
    { 
     cout << "Error opening file"; 
    } 


    return 0; 
} 

這裏的文本文件的前幾行:

aback, to take = surprizi. 
abaft = posta parto. 
abandon = forlasi. 
abase = humiligi. [error in book: humilgi] 
abash = hontigi. 
abate (lower) = mallevi. 
abate (speed) = malakceli. 
abbey = abatejo. 
abbot = abato. 
abbreviate = mallongigi. 
abdicate = demeti la reĝecon. 
abdomen = ventro. 

感謝您的期待。

+3

你可以用'的std ::函數getline(infile的,一句話,「= ')'讀取鍵和'std :: getline(infile,word);'獲得行的其餘部分。 –

回答

2

正如評論所說的@Thomas,爲第一getline設置'='作爲分隔符,以及默認的分隔符換行符爲第二getlineDemo.

string before_equal, after_equal; 
string arr[2]; 
while (getline(cin, before_equal, '=') && getline(cin, after_equal)) 
{ 
    stringstream ssin1(before_equal); 
    stringstream ssin2(after_equal); 
    if (ssin1.good() && ssin2.good()) { 
     ssin1 >> arr[0]; 
     ssin2 >> arr[1]; 
    } 
    else continue; 
    cout << arr[0] << ' ' << arr[1]; 
    cout << endl; 
} 
0

如果您有機會獲得提升,Boost.Tokenizer是你在找什麼。 你可以找到一個例子here。 這將返回一個字符串的向量。

否則,您可以使用std::string::find,並執行這些方針的東西:

int indOfDelimiter=line.find('='); 
std::string Key = line.substr(0,indOfDelimiter); 
std::string Value = line.substr(indOfDelimiter+1); 

在您的例子,也有前後「=」的空間。你可能想通過改變上面的代碼中的分隔符擺脫那些:

const char *delimiter = " = "; 
int indOfDelimiter=line.find(delimiter); 
std::string Key = line.substr(0,indOfDelimiter); 
std::string Value = line.substr(indOfDelimiter+3); 
+1

我認爲'Boost.Tokenizer'可能是每一行只有兩個項目矯枉過正。 –

+0

@ThomasMatthews可能是的,但它是一個值得關注的乾淨而強大的工具。 –

1

您可以使用類std::string的標準數據成員函數來執行任務。結果對可以存儲在標準類std::vector

這裏是一個示範項目

#include <iostream> 
#include <string> 
#include <vector> 
#include <utility> 
#include <cstring> 

std::string & trim(std::string &s) 
{ 
    std::string::size_type n = 0; 

    while (n < s.size() && std::isspace((unsigned char)s[n])) n++; 

    s.erase(0, n); 

    n = s.size(); 

    while (n != 0 && std::isspace((unsigned char)s[n-1])) n--; 

    s.erase(n); 
} 

int main() 
{ 
    const char * s[] = 
    { 
     "aback, to take = surprizi.", 
     "abaft = posta parto.", 
     "abandon = forlasi.", 
     "abase = humiligi. [error in book: humilgi]", 
     "abash = hontigi.", 
     "abate (lower) = mallevi.", 
     "abate (speed) = malakceli.", 
     "abbey = abatejo.", 
     "abbot = abato.", 
     "abbreviate = mallongigi.", 
     "abdicate = demeti la reĝecon.", 
     "abdomen = ventro.", 
    }; 


    std::vector< std::pair<std::string, std::string>> dictionary; 

    for (std::string line : s) 
    { 
     auto n = line.find('='); 

     std::string key, description; 

     if (n == std::string::npos) 
     { 
      key = line; 
     } 
     else 
     { 
      key = line.substr(0, n); 
      description = line.substr(n + 1); 
     } 

     trim(key); trim(description); 

     dictionary.push_back({ key, description }); 
    } 


    for (const auto &p : dictionary) 
    { 
     std::cout << p.first << '\t' << p.second << std::endl; 
    } 

    return 0; 
} 

它的輸出是

aback, to take surprizi. 
abaft posta parto. 
abandon forlasi. 
abase humiligi. [error in book: humilgi] 
abash hontigi. 
abate (lower) mallevi. 
abate (speed) malakceli. 
abbey abatejo. 
abbot abato. 
abbreviate mallongigi. 
abdicate demeti la reĝecon. 
abdomen ventro. 
相關問題