使用逗号作为分隔符的 C++ getline 多个变量类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19107439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:31:01  来源:igfitidea点击:

C++ getline multiple variable types using comma as delimiter

c++csvgetline

提问by user2661167

I'm trying to do a home work assignment which requires data fro a txt file to be read in to variables. The file has this on each line "surname, initials, number, number". I have got the get line working partially using the following code.

我正在尝试做一个家庭作业,它需要将 txt 文件中的数据读入变量。该文件的每一行都有这个“姓氏,姓名首字母,数字,数字”。我已经使用以下代码使 get 线部分工作。

    ifstream inputFile("Students.txt");
string line;

string Surname;
string Initial;
int number1, number2;

while (getline(inputFile, line))
{
    stringstream linestream(line);

    getline(linestream, Surname, ',');
    getline(linestream, Initial, ',');
    getline(linestream, number1, ',');
    getline(linestream, number2, ',');

    cout << Surname << "---" << Initial << "-" << number1 << "-" << number2 << endl;

}

This throws a compile error, but if I declare number1 and number2 as strings it works fine. So my question is, do I have to getline as a string then convert to an int variable or is there a better way?

这会引发编译错误,但如果我将 number1 和 number2 声明为字符串,它就可以正常工作。所以我的问题是,我是否必须将 getline 作为字符串然后转换为 int 变量还是有更好的方法?

回答by Oli_G

Yes, the second parameter of getline function must be a string by definition and it will contain your extracted string. Simply declare number1 and number2 as string and then convert them to Integer with stoi() (C++11) or atoi() function :

是的,getline 函数的第二个参数必须是定义的字符串,它将包含您提取的字符串。只需将 number1 和 number2 声明为字符串,然后使用 stoi() (C++11) 或 atoi() 函数将它们转换为整数:

string strNumber1;
string strNumber2;
getline(linestream, strNumber1, ',');
getline(linestream, strNumber2, ',');
int number1 = stoi(strNumber1);
int number2 = atoi(strNumber2.c_str());

Hope this helps

希望这可以帮助

回答by 0x499602D2

std::getlinetakes as a first parameter an object of std::basic_istream. It won't work for any other object.

std::getline将 的对象作为第一个参数std::basic_istream。它不适用于任何其他对象。

What I did was use the csv_whitespaceclass to add a comma as a delimeter. For example:

我所做的是使用csv_whitespace该类添加逗号作为分隔符。例如:

class csv_whitespace
    : public std::ctype<char>
{
public:
    static const mask* make_table()
    {
        static std::vector<mask> v(classic_table(), classic_table() + table_size);
        v[','] |= space;
        v[' '] |= space;
        return &v[0];
    }

    csv_whitespace(std::size_t refs = 0) : ctype(make_table(), false, refs) { }
};

int main()
{
    std::ifstream in("Students.txt");
    std::string line;

    std::string surname;
    std::string initial;
    int number1, number2;

    while (std::getline(in, line))
    {
        std::stringstream linestream(line);
        linestream.imbue(std::locale(linestream.getloc(), new csv_whitespace));

        getline(linestream, surname, ',');
        getline(linestream, initial, ',');

        linestream >> number1 >> number2;
    }
}