I have a question about reading the content of CSV files inside C++. Basically I have a tile containing data saved in CSV format. What I would like to do is being able to read those data and allocate everything in memory on a matrix, or separate each column into a separated vector.
The first thing I am trying to do is, getting the number of lines and print the content on the consolle.
I do this by getline(stream, line, separator):
if(myFile.is_open())
{
while(!myFile.eof())
{
std::getline(myFile, line, ',');
std::cout << n_Lines << ") " << line << std::endl;
n_Lines = n_Lines + 1;
}
}
Now the problem is that in this way the parsing takes the commas as separators, but takes the /n (newline) into account and appends it to each last number of a row:
198) 86
199) 47
46
200) 53
201) 58
202) 4
203) 62
204) 90
205) 98
206) 58
207) 39
208) 4
34
209) 70
210) 58
211) 33
212) 8
213) 73
214) 20
215) 61
216) 9
217) 76
6
218) 22
in this cas n_Lines should count the elements, but once every ten elements, two numbers are stuck together as a whole string.
How can I avoid this and parse my file correctly? Is there a more efficient way to do this and save my data maybe directly into a matrix?
Thanks, Gio