2015-03-18 45 views
0

我有一個文本文件,該文件我想在閱讀和分頭然後創建一個新對象出數據。,由多個分隔符在下分離++

我發現這個代碼:

std::ifstream file("plop"); 
std::string line; 

while(std::getline(file, line)) 
{ 
    std::stringstream linestream(line); 
    std::string   data; 
    int     val1; 
    int     val2; 

    std::getline(linestream, data, '\t'); 

    linestream >> val1 >> val2; 
} 

在一個文本文件讀取,並通過線將其分解。但是,此代碼假定分隔符總是一個選項卡。如果數據有多個分隔符會指向哪種類型的數據將會如何。即假設一個文本文件如:

hey, "hi" (hello) [hola] 
bye, "by" (byeee) [biii] 

,我想將數據與分隔符是兩個「 和

threeCharacters = hey and bye 
分成

String twoCharacters; 
String threeCharacters; 
String fourCharacters; 
String fiveCharacters; 

所以

twoCharacters = hi and by 

用分隔符是一個,之後

任何幫助將不勝感激!謝謝。

回答

1

你可以只保留打電話std::getline()分隔符不同:

std::ifstream file("test.txt"); 

std::string line; 
while(std::getline(file, line)) 
{ 
    std::stringstream linestream(line); 

    std::string skip; 
    std::string item1; 
    std::string item2; 
    std::string item3; 
    std::string item4; 

    std::getline(linestream, item1, ','); 
    std::getline(linestream, skip, '"'); 
    std::getline(linestream, item2, '"'); 
    std::getline(linestream, skip, '('); 
    std::getline(linestream, item3, ')'); 
    std::getline(linestream, skip, '['); 
    std::getline(linestream, item4, ']'); 

    if(linestream) // true if there were no errors reading the stream 
    { 
     std::cout << item1 << '\n'; 
     std::cout << item2 << '\n'; 
     std::cout << item3 << '\n'; 
     std::cout << item4 << '\n'; 
    } 
} 

我用可變skip讀取到下一個字段的開頭。

+0

什麼是變量'iss'?它在if條件中使用。 – RichS 2017-09-06 22:22:59

+0

@RichS Ahah,這是一個錯誤,現在已經糾正。日Thnx。 – Galik 2017-09-06 22:33:18

0

您可以使用std::istream::sentry來做到這一點。雖然有點複雜,但您幾乎可以完全控制數據的輸入方式。

#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <sstream> 
#include <map> 

std::istream& operator>>(std::istream& is, 
     std::map<std::string, std::vector<std::string> >& map) { 
    std::istream::sentry s(is); 
    if(s) { 
     // your accumulator 
     std::string str1; 
     // while your stream is good keep appending data 
     while(is.good()) { 
      // get the next character in the stream 
      char c = is.get(); 
      // if it is a comma append the data to the "three" vector 
      if(c == ',') { 
       map["three"].push_back(str1); 
      // if it is a comma get all of the data until the next comma 
      } else if(c == '"') { 
       std::string str2; 
       str2 += c; 
       c = is.get(); 
       while(c != '"') { 
        str2 += c; 
        c = is.get(); 
       } 
       str2 += c; 
       map["two"].push_back(str2); 
      // do the same thing for parenthases 
      } else if(c == '(') { 
       std::string str2; 
       str2 += c; 
       c = is.get(); 
       while(c != ')') { 
        str2 += c; 
        c = is.get(); 
       } 
       str2 += c; 
       map["five"].push_back(str2); 
      // do the same thing for square brackets 
      } else if(c == '[') { 
       std::string str2; 
       str2 += c; 
       c = is.get(); 
       while(c != ']') { 
        str2 += c; 
        c = is.get(); 
       } 
       str2 += c; 
       map["four"].push_back(str2); 
      // append to your accumulator if you get an alphanumeric char 
      } else if(std::isalnum(c)) { 
       str1 += c; 
      // clear your accumulator if you get a return 
      } else if(c == '\n') { 
       str1.clear(); 
      } 
     } 
    } 
    return(is); 
} 

int main() { 
    std::ifstream file("plop"); 
    // make your map to hold your data 
    std::map<std::string, std::vector<std::string> > map; 
    map["two"] = std::vector<std::string>(); 
    map["three"] = std::vector<std::string>(); 
    map["four"] = std::vector<std::string>(); 
    map["five"] = std::vector<std::string>(); 

    // read your data 
    file >> map; 

    // print your data 
    std::vector<std::string> words{"two", "three", "four", "five"}; 

    for(auto x: words) { 
     std::cout << x << std::endl; 
     for(auto y: map[x]){ 
      std::cout << '\t' << y << std::endl; 
     } 
    } 
} 

這應該打印

two 
    "hi" 
    "by" 
three 
    hey 
    bye 
four 
    [hola] 
    [biii] 
five 
    (hello) 
    (byeee)