具有多个 EOL 字符的常规 CSV 解析器
General CSV Parser with multiple EOL characters
我正在尝试更改此功能,以考虑何时给出带有r
结尾的 CSV 文件。我似乎不知道如何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;
};
这可能是 Get std :: ifstream 处理 LF、CR 和 CRLF 的副本?。最上面的答案特别好。
如果您知道每行都以r
结尾,则始终可以使用 getline(input, data, '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
相关文章:
- 正在将csv文件读取为双精度矢量
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 如何选择在 csv 文件中输出的位置
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- WinAPI 检查窗口是否具有常规标题栏
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 无法读取C++中的 CSV 文件
- 流:CSV 文件中的换行符
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 将一维数组写入 CSV C++中的不同列?
- 将矢量设置为常规参数
- 如何从CSV加载堆栈?C++
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 如何正确格式化csv文件?
- 正在分析CSV文件-C++
- 将通用Windows应用程序转换为VisualStudio 2019上的常规应用程序
- 如何获取组件和搜索 CSV 文件
- G++ 编译器是否在未使用返回值的情况下将 constexpr 函数视为常规函数?
- 具有多个 EOL 字符的常规 CSV 解析器