我正在尝试将csv文件读入C ++,但我得到的消息是"程序以退出代码结束:255"。我尝试重新启动Xcode,重启Mac,但它没有用。有人有同样的问题吗?
这是我的代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
using namespace std;
// defind the type of longitude and latitude as the key
typedef struct {
double lat;
double lon;
} location;
struct Stud{
location key;
string date;
double temp;
double concen;
string descrip;
};
int main()
{
int counter = 0;
string line;
Stud items;
ifstream file("data.csv");
if( !file.is_open()) {
cout << "File not found." << endl;
exit(-1);
}
while ( getline( file, line) ) { // To get the number of lines in the file
counter++;
}
Stud* item = new Stud[counter]; // Add number to structured array
for (int i = 0; i < counter; i++) {
file >> item[i].date >> item[i].key.lon>> item[i].key.lat>> item[i].temp >> item[i].concen >> item[i].descrip;
}
cout << item[1].date << endl;
file.close();
return 0;
}
我感谢任何帮助!