PDA

View Full Version : CSV-File



durus90
26th July 2017, 14:20
Hello!

I have a csv file:
1, 2, 3, 4,
5, 6, 7, 8,
with 4 columns and int values on each column.
I read it line by line.

I'd wish now to take the first and the second value from each line, and give those values to a struct, and then return a QStringList of those Structures.


typedef struct Coordinates{
float latitude; //this will be 1 and 5
float longitude; // this will be 2 and 6
}t_Coordinates;


I dont know how to acces specific values of a line.
I'm sorry if I didn't make this thread properly, I'm very new to this, and I am still learning.

Alex2822
26th July 2017, 14:46
You could use a for loop to navigate through each line and store each value in an array/vector, then access to the 2nd or 4th element



vector<float> exmpleArray;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
exempleArray.pushback(column[j]);
}
}

It would look like this.

jefftee
26th July 2017, 21:44
If your input file only contains numbers and commas (i.e. no quoted strings), then you could simply split the string on the comma... See QString::split() and QStringList.

PierreA
27th July 2017, 00:24
Properly reading a general CSV file (which can contain quoted strings) is a bit more complicated. If a comma is preceded by an odd number of quotation marks, it is part of a string, not a separator. And if a quotation mark is immediately after another quotation mark, which is preceded by an odd number of quotation marks, then it too, is part of a string. If you are reading CSV files that may contain quoted strings, you should do this. If not, just split at commas.

Depending on how precisely you need to locate a point on the earth, float may not be precise enough. I use 32-bit int for most angles, which locates a point to the nearest 18 mm on a meridian and 18 mm on the equator (the precision on other parallels varies with latitude). For more precise positioning, I use double, expressing both angles in radians.

jefftee
27th July 2017, 00:39
I certainly understand that. The OP's original post showed simple numbers in the input, which is why I prefaced my reply with "If your input file contains number and commas (i.e. no quoted strings)...".