I am trying to read a .csv file which has around 50-thousands lines. In the code below I am reading each one line at a time, parsing it into pre-define struct called NinjaInfo_t
and then pushing it into a list. The whole process of reading this file took me more than 1 minute. Is there a another way for it to be faster ? Also this is a homework so i cannot use any libraries that support reading csv file and I have to use a linked list to store data.
ifstream is("data.csv",ifstream::binary);
is.seekg(0, ios_base::end);
size_t size = is.tellg();
is.seekg(0, ios_base::beg);
char* buffer = new char[size];
is.read(buffer, size);
stringstream ss;
ss.str(buffer);
// skip the file line in data.csv
string data;
getline(ss, data);
while (getline(ss, data)) {
NinjaInfo_t newData;
stringstream dataStream;
dataStream.str(data);
string tmp;
string timestamp;
string id;
string longtitude, latitude;
getline(dataStream, tmp, ',');
getline(dataStream, timestamp, ',');
getline(dataStream, id, ',');
getline(dataStream, longtitude, ',');
getline(dataStream, latitude, ',');
getline(dataStream, tmp);
istringstream(longtitude) >> newData.longitude;
istringstream(latitude) >> newData.latitude;
while (id.length() < 4) {
id = '0' + id;
}
istringstream(id) >> newData.id;
stringstream ss;
struct tm _tm = {};
string t = timestamp;
ss.str(t);
ss >> std::get_time(&_tm, "%d/%m/%Y %H:%M:%S");
_tm.tm_isdst = -1;
time_t time = std::mktime(&_tm);
newData.timestamp = time;
db.push_back(newData); //DB IS A LINKED LIST.
P.s: sorry for the bad english :P.