2017-02-27 63 views
0

我需要將字符串解析爲具有不同數據類型(整數,字符串)的變量。C++將字符串解析爲不同的數據類型變量

問題中的字符串是從文件中的一行中獲取的。

我想知道是否有類似於inFile >> var1 >> var2 >> etc的函數;我可以使用字符串。以下是文件的完整行。

2016年12月6日的「政府和大企業之間的亂倫關係在黑暗中繁盛。〜傑克·安德森[4]」 0 3 39藍白PATRICK巴德韋爾[email protected]

我已經分配了「2016/12/6」,「s」以及引號和變量之間的所有使用inFile >>;的內容。另外,在雙引號的最後出現後,我將所有內容都存儲在字符串restOfLine中。現在,我想將restOfLine解析爲每個值的變量(0,3,39,blue,white,Patrick,Bardwell,[email protected]都應該是單獨的變量)。有沒有像inFile這樣的方法可以用來做到這一點?我還嘗試將它們與restOfline.find()和restOfLine.substr()分開,但一直未能弄清楚。同樣,如果我可以比我當前的代碼更有效地將整個行中的每個值分開,我寧願那樣做。下面的當前代碼。任何幫助深表感謝。

int main() 
{ 

    // Declare variables 
    string userFile; 
    string line; 
    string date; 
    char printMethod; 
    string message; 
    int numMedium; 
    int numLarge; 
    int numXL; 
    string shirtColor; 
    string inkColor; 
    string firstName; 
    string lastName; 
    string customerEmail; 
    string firstLine; 
    string restOfLine; 


    // Prompt user to 'upload' file 

    cout << "Please input the name of your file:\n"; 
    cin >> userFile; 
    fstream inFile; 
    inFile.open(userFile.c_str()); 

    // Check if file open successful -- if so, process 

    if (inFile.is_open()) 
    { 
     getline(inFile, firstLine); // get column headings out of the way 
     cout << firstLine << endl << endl; 

     while(inFile.good()) 
// while we are not at the end of the file, process 
     { 

      getline(inFile, line); 

      inFile >> date >> printMethod; // assigns first two values of line to date and printMethod, respectively 

      int pos1 = line.find("\""); 
// find first occurrence of a double quotation mark and assign position value to pos1 
      int pos2 = line.rfind("\""); 
// find last occurrence of a double quotation mark and assign position value to pos2 

      string message = line.substr(pos1, (pos2 - pos1)); 
// sets message to string between quotation marks 

      string restOfLine = line.substr(pos2 + 2); 
// restOfLine = everything after the message -- used to parse 

     } 

     inFile.close(); 
    } 

    // If file open failure, output error message, exit with return 0; 

    else 
    { 

     cout << "Error opening file"; 

    } 

    return 0; 

} 
+0

您是否已經知道[stringstream](http://stackoverflow.com/questions/20594520/what-exactly-does-stringstream-do)? –

回答

0
#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 
using namespace std; 
unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch); 
    int main(int argc, const char * argv[]) { 

    string text = "2016/12/6 s \"The incestuous relationship between government and big business thrives in the dark. ~Jack Anderson [4]\" 0 3 39 blue white PATRICK BARDWELL [email protected] "; 
    std::vector<std::string> v; 

    split(text, v, ' '); 
    return 0; 
} 

unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch) 
{ 
    unsigned int pos = static_cast<unsigned int>(txt.find(ch)); 
    unsigned int initialPos = 0; 
    strs.clear(); 

    // Decompose statement 
    while(pos >! txt.size()) { 
     strs.push_back(txt.substr(initialPos, pos - initialPos + 1)); 
     initialPos = pos + 1; 

     pos = static_cast<unsigned int>(txt.find(ch, initialPos)); 
     if(pos > txt.size()) break; 
    } 

    // Add the last one 
// strs.push_back(txt.substr(initialPos, std::min(pos, static_cast<unsigned int>(txt.size())) - initialPos + 1)); 

    return static_cast<unsigned int>(strs.size()); 
} 

所以上述程序將擊穿的串入部件然後使用下面提及函數數據類型轉換。 要將字符串轉換爲int,您可以使用std :: stoi(str),這在C++ 11中可用。 所有類型的數字都有版本:long stol(string),float stof(string),double stod(string),... 有關更多信息,請參閱http://en.cppreference.com/w/cpp/string/basic_string/stol

1

我想知道是否有一個函數類似inFile >> var1 >> var2 >> etc;我可以使用字符串?

是的,而不僅僅是相似的,實際上是相同的。字符串流的工作方式與文件流相同。

std::stringstream ss(restOfLine); 
ss >> numMedium >> numLarge >> numXL >> shirtColor >> inkColor >> firstName >> lastName >> customerEmail >> firstLine; 
相關問題