【发布时间】:2015-05-20 14:35:48
【问题描述】:
我正在尝试读取包含以下条目的 CSV 文件:
//2009-12-31 21:00:00, COUNTRY ,1.84296,350.947,60.72
这就是我所做的
#include <iostream>
#include <fstream>
#include <sstream>
int main()
{
using namespace std;
ifstream read("data.csv");
string line;
//I want to use this to hold the data temporarily
string temp[5];
while (std::getline(read, line))
{
int i=0;
std::istringstream iss(line); // string stream
while(std::getline(iss, temp[i], ','))
{
cout << i << ": " << temp[i] << endl;
++i;
};
}
}
但它没有做我希望代码做的事情。特别是,代码在我达到 21 的整数后停止。这是输出
0: 2009-12-31 21:00:00 1: GRID_A 2: 1.84296 3: 350.947 4: 60.72 2010-01-01 00:00:00 5: GRID_A 6: 1.93569 7: 348.98 8: 60.64 2010-01-01 03:00:00 9: GRID_A 10: 2.30688 11: 339.444 12: 247.6 2010-01-01 06:00:00 13: GRID_A 14: 1.74453 15: 326.219 16: 587.92 2010-01-01 09:00:00 17: GRID_A 18: 2.16002 19: 289.19 20: 180.72 2010-01-01 12:00:00 21: GRID_A
然后我收到这样的错误
_LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;} Thread 1: EXC_BAD_ACCESS...
你能告诉我有什么问题吗?非常感谢!
PS:原来问题与我在 Mac 上使用 Excel 保存的 CSV 文件有关。缺少换行符。
【问题讨论】:
-
我会使用正则表达式。
-
如果你不能提供有问题的例子和预期的输出,不要指望太多帮助。与实际错误相比,找到可能的错误要困难得多。
-
看看第 22 行。你的问题可能就在那里。