简体   繁体   English

使用提取运算符简化从逗号文件中读取?

[英]simplify reading from file with comma using extraction operator?

I want to read from a file that consists of entries in each line that look like this:我想从一个文件中读取,该文件由每行中的条目组成,如下所示:

BA,1355,SIN,3316,MEL,3339,Y,0,744
BA,1355,SIN,3316,LHR,507,,0,744 777

I want to store the first 6 columns and save them to string and int respectively.我想存储前 6 列并将它们分别保存到 string 和 int 。

At the moment, I have use getline to handle the comma, as shown below.目前,我已经使用getline来处理逗号,如下所示。 But the drawbacks are 1) getline can only store things in string, so the numbers are saved as string instead of int 2) the code is repetitive但是缺点是 1) getline只能用字符串存储东西,所以数字保存为字符串而不是 int 2)代码是重复的

Is there a way that I can use the and string::ignore and >> operator in stringstream to accomplish the same thing?有没有办法可以在stringstream中使用 and string::ignore>>运算符来完成同样的事情? something like linestream>> airline >> airlineNo >> scr >> scrNum >> det >>detNum;类似于linestream>> airline >> airlineNo >> scr >> scrNum >> det >>detNum; where airlineNo , scrNum and detNum are numbers?哪里airlineNoscrNumdetNum是数字?

while (getline(myfile, temp)) {
        //std::cout<<temp<<std::endl;
        std::stringstream linestream(temp);
        std::string line;

        std::string airline, scr, det; 
        std::string airlineNo, scrNum, detNum;

        getline(linestream, airline, ',');
        getline(linestream, airlineNo, ',');
        getline(linestream, scr, ',');
        getline(linestream, scrNum, ',');
        getline(linestream, det, ',');
        getline(linestream, detNum, ',');
        
        std::cout<<scr<<" "<<det<<std::endl;
        
    }

You can certainly use operator>> with a std::(i)stringstream , eg:您当然可以将operator>>std::(i)stringstream一起使用,例如:

while (getline(myfile, temp)) {
    //std::cout << temp << std::endl;
    std::istringstream linestream(temp);

    std::string airline, scr, det; 
    int airlineNo, scrNum, detNum;

    getline(linestream, airline, ',');
    linestream >> airlineNo;
    linestream.ignore(std::numeric_limits<std::streamsize>::max(), ',');
    getline(linestream, scr, ',');
    linestream >> scrNum;
    linestream.ignore(std::numeric_limits<std::streamsize>::max(), ',');
    getline(linestream, det, ',');
    linestream >> detNum;
        
    std::cout << scr << " " << det << std::endl;      
}

Alternatively, just use std::stoi() on the strings that std::getline() outputs, eg:或者,只需在std::getline()输出的字符串上使用std::stoi() ,例如:

while (getline(myfile, temp)) {
    //std::cout << temp << std::endl;
    std::istringstream linestream(temp);

    std::string airline, scr, det; 
    int airlineNo, scrNum, detNum;

    getline(linestream, airline, ',');
    getline(linestream, temp, ‘,’);
    airlineNo = std::stoi(temp);
    getline(linestream, scr, ',');
    getline(linestream, temp, ‘,’);
    scrNum = std::stoi(temp);
    getline(linestream, det, ',');
    getline(linestream, temp, ‘,’);
    detNum = std::stoi(temp);
        
    std::cout << scr << " " << det << std::endl;      
}

If you're sure the first six fields are going to be filled, what you could do is replace every comma in a line to spaces, so that you can then use operator<< to get objects nicely.如果您确定前六个字段将被填充,您可以做的是将一行中的每个逗号替换为空格,以便您可以使用 operator<< 很好地获取对象。 This doesn't work if any of the important fields are empty.如果任何重要字段为空,这将不起作用。

for (int i = 0; i < temp.size(); ++i) {
    if (temp[i] == ',') temp[i] = ' ';
}
std::stringstream linestream(temp);
/*...*/
linestream >> airline >> airlineNo >> scr >> scrNum >> det >> detNum;    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM