为什么 getline() 会切断 CSV 输入?
Why does getline() cut off CSV Input?
我正在尝试用 C++ 读取和解析我的 CSV 文件,运行 出现错误。
CSV 包含 1-1000 行,始终为 8 列。
通常我想做的是读取 csv 并仅输出符合过滤条件的行。例如第 2 列是时间戳,仅在特定时间 运行ge.
我的问题是我的程序切断了一些行。
在数据在字符串记录变量中的位置,它不是截止点。一旦我将它推入 int/vector 的地图,它的截止点。我是不是做错了什么?
有人可以帮助我确定真正的问题所在,或者甚至可以给我一个更好的方法吗?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <map>
#include "csv.h"
using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;
using std::istringstream;
string readFileIntoString(const string& path) {
auto ss = ostringstream{};
ifstream input_file(path);
if (!input_file.is_open()) {
cerr << "Could not open the file - '"
<< path << "'" << endl;
exit(EXIT_FAILURE);
}
ss << input_file.rdbuf();
return ss.str();
}
int main()
{
int filterID = 3;
int filterIDIndex = filterID;
string filter = "System";
/*Filter ID's:
0 Record ID
1 TimeStamp
2 UTC
3 UserID
4 ObjectID
5 Description
6 Comment
7 Checksum
*/
string filename("C:/Storage Card SD/Audit.csv");
string file_contents;
std::map<int, std::vector<string>> csv_contents;
char delimiter = ',';
file_contents = readFileIntoString(filename);
istringstream sstream(file_contents);
std::vector<string> items;
string record;
int counter = 0;
while (std::getline(sstream, record)) {
istringstream line(record);
while (std::getline(line, record, delimiter)) {
items.push_back(record);
cout << record << endl;
}
csv_contents[counter] = items;
//cout << csv_contents[counter][0] << endl;
items.clear();
counter += 1;
}
我看不出你的数据被裁剪的原因,但我已经稍微重构了你的代码并使用它可能更容易调试问题,如果它不会自行消失的话.
int main()
{
string path("D:/Audit.csv");
ifstream input_file(path);
if (!input_file.is_open())
{
cerr << "Could not open the file - '" << path << "'" << endl;
exit(EXIT_FAILURE);
}
std::map<int, std::vector<string>> csv_contents;
std::vector<string> items;
string record;
char delimiter = ';';
int counter = 0;
while (std::getline(input_file, record))
{
istringstream line(record);
while (std::getline(line, record, delimiter))
{
items.push_back(record);
cout << record << endl;
}
csv_contents[counter] = items;
items.clear();
++counter;
}
return counter;
}
我试过你的代码(修复定界符后)没有问题,但我只有三行数据,所以如果是内存问题,它不太可能显示。
我正在尝试用 C++ 读取和解析我的 CSV 文件,运行 出现错误。
CSV 包含 1-1000 行,始终为 8 列。
通常我想做的是读取 csv 并仅输出符合过滤条件的行。例如第 2 列是时间戳,仅在特定时间 运行ge.
我的问题是我的程序切断了一些行。
在数据在字符串记录变量中的位置,它不是截止点。一旦我将它推入 int/vector 的地图,它的截止点。我是不是做错了什么?
有人可以帮助我确定真正的问题所在,或者甚至可以给我一个更好的方法吗?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <map>
#include "csv.h"
using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;
using std::istringstream;
string readFileIntoString(const string& path) {
auto ss = ostringstream{};
ifstream input_file(path);
if (!input_file.is_open()) {
cerr << "Could not open the file - '"
<< path << "'" << endl;
exit(EXIT_FAILURE);
}
ss << input_file.rdbuf();
return ss.str();
}
int main()
{
int filterID = 3;
int filterIDIndex = filterID;
string filter = "System";
/*Filter ID's:
0 Record ID
1 TimeStamp
2 UTC
3 UserID
4 ObjectID
5 Description
6 Comment
7 Checksum
*/
string filename("C:/Storage Card SD/Audit.csv");
string file_contents;
std::map<int, std::vector<string>> csv_contents;
char delimiter = ',';
file_contents = readFileIntoString(filename);
istringstream sstream(file_contents);
std::vector<string> items;
string record;
int counter = 0;
while (std::getline(sstream, record)) {
istringstream line(record);
while (std::getline(line, record, delimiter)) {
items.push_back(record);
cout << record << endl;
}
csv_contents[counter] = items;
//cout << csv_contents[counter][0] << endl;
items.clear();
counter += 1;
}
我看不出你的数据被裁剪的原因,但我已经稍微重构了你的代码并使用它可能更容易调试问题,如果它不会自行消失的话.
int main()
{
string path("D:/Audit.csv");
ifstream input_file(path);
if (!input_file.is_open())
{
cerr << "Could not open the file - '" << path << "'" << endl;
exit(EXIT_FAILURE);
}
std::map<int, std::vector<string>> csv_contents;
std::vector<string> items;
string record;
char delimiter = ';';
int counter = 0;
while (std::getline(input_file, record))
{
istringstream line(record);
while (std::getline(line, record, delimiter))
{
items.push_back(record);
cout << record << endl;
}
csv_contents[counter] = items;
items.clear();
++counter;
}
return counter;
}
我试过你的代码(修复定界符后)没有问题,但我只有三行数据,所以如果是内存问题,它不太可能显示。