Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有多个EOL字符的通用CSV解析器_C++_Parsing_Csv - Fatal编程技术网

C++ 具有多个EOL字符的通用CSV解析器

C++ 具有多个EOL字符的通用CSV解析器,c++,parsing,csv,C++,Parsing,Csv,我正在尝试更改此函数,以同时考虑CSV文件以\r结尾时的情况。我似乎不知道如何获取getline()考虑到这一点 vector<vector<string>> Parse::parseCSV(string file) { // input fstream instance ifstream inFile; inFile.open(file); // check for error if (inFile.fail()) { cerr

我正在尝试更改此函数,以同时考虑CSV文件以
\r
结尾时的情况。我似乎不知道如何获取
getline()
考虑到这一点

vector<vector<string>> Parse::parseCSV(string file)
{
    // input fstream instance
    ifstream inFile;
    inFile.open(file);

    // check for error
    if (inFile.fail()) { cerr << "Cannot open file" << endl; exit(1); }

    vector<vector<string>> data;
    string line;

    while (getline(inFile, line))
    {
        stringstream inputLine(line);
        char delimeter = ',';
        string word;
        vector<string> brokenLine;
        while (getline(inputLine, word, delimeter)) {
            word.erase(remove(word.begin(), word.end(), ' '), word.end());      // remove all white spaces
            brokenLine.push_back(word);
        }
        data.push_back(brokenLine);
    }

    inFile.close();

    return data;

};
vector Parse::parseCSV(字符串文件)
{
//输入流实例
河流充填;
inFile.open(文件);
//检查错误

如果(infle.fail()){cerr这可能是的重复。最上面的答案特别好

如果知道每一行都以
\r
结尾,则始终可以使用
getline(输入,数据,'\r')
指定
getline的分隔符,其中输入是一个流,数据是一个字符串,第三个参数是要拆分的字符。在第一个while循环开始后,还可以尝试以下操作

// after the start of the first while loop
stringstream inputLine;
size_t pos = line.find('\r');
if(pos < line.size()) {
    inputLine << std::string(x.begin(), x.begin() + p);
    inputLine << "\n"
    inputLine << std::string(x.begin() + p + 1, x.end());
} else {
    inputLine << line;
}
// the rest of your code here
//在第一个while循环开始后
字符串流输入线;
size_t pos=line.find('\r');
如果(位置