C ++将字符串解析为不同的数据类型变量

时间:2017-02-27 09:10:58

标签: c++ string file parsing

我需要将字符串解析为具有不同数据类型(整数,字符串)的变量。

相关字符串取自文件中的一行。

我想知道是否有类似于inFile的功能>> var1>> var2>>等等;我可以用于字符串。以下是该文件的完整一行。

2016/12/6 s“政府和大企业之间的乱伦关系在黑暗中茁壮成长.~杰克安德森[4]”0 3 39蓝白PATRICK BARDWELL pat.bardwell@bwpmlp.com

我已经使用inFile>>分配了“2016/12/6”,“s”以及引号与变量之间的所有内容。 ;。此外,我在最后出现双引号后将所有内容都存入并将其存储到字符串restOfLine中。现在,我想将restOfLine解析为每个值的变量(0,3,39,蓝色,白色,Patrick,Bardwell,pat.bardwell @ bwpmlp.com都应该是单独的变量)。是否有像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;

}

2 个答案:

答案 0 :(得分:1)

  

我想知道是否有类似于inFile的功能&gt;&gt; var1&gt;&gt; var2&gt;&gt;等等;我可以用作字符串吗?

是的,而不仅仅是相似,事实相同。字符串流与文件流的工作方式相同。

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

答案 1 :(得分: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 pat.bardwell@bwpmlp.com ";
    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