I’m parsing a CSV file that looks like this:
E1,E2,E7,E8,,,
E2,E1,E3,,,,
E3,E2,E8,,,
E4,E5,E8,E11,,,
I store the first entry in each line in a string, and the rest go in a vector of strings:
while (getline(file_input, line)) {
stringstream tokenizer;
tokenizer << line;
getline(tokenizer, roomID, ',');
vector<string> aVector;
while (getline(tokenizer, adjRoomID, ',')) {
if (!adjRoomID.empty()) {
aVector.push_back(adjRoomID);
}
}
Room aRoom(roomID, aVector);
rooms.addToTail(aRoom);
}
In windows this works fine, however in Linux the first entry of each vector mysteriously loses the first character. For Example in the first iteration through the while loop:
roomID would be E1 and aVector would be 2 E7 E8
then the second iteration:
roomID would be E2 and aVector would be 1 E3
Notice the missing E’s in the first entry of aVector.
when I put in some debugging code it appears that it is initially being stored correctly in the vector, but then something overwrites it. Kudos to whoever figures this one out. Seems bizarre to me.
EDIT:
thank you Erik. I finally understand. On windows all the lines just end with a \n. When I switch to Unix\Linux however, the lines end in \r\n. Thus, when getline reads a line it reads everything into the string including the \r. I was not accounting for this \r and it was screwing me up. The problem wasn’t that the E was missing. It was that I had an extra entry in the vector with a single \r character in it. My other classes couldn’t handle this entry with a single \r in it.
I suspect that the \r in the windows \r\n linefeed could mess up the code doing your printing.
If you change to this if statement, does the problem disappear?
EDIT: Fixed typo