Read binary file into 2 QVectors
Hello, I have one question about reading binary files.
I wrote data like this:
Code:
QVector<QPoint> p;
out << p;
..and tried reading it this way:
Code:
QVector<qint32> x, y;
in >> x >>y;
I know this isn't the right way to read this kind of data,
so can anyone help me to put binary data into two vectors, x and y.
Data is written into binary file as a list of points: x1 y1 x2 y2 x3 y3..
..and i should read first all x coordinates to x vector and all y coordinates to y vector..
Re: Read binary file into 2 QVectors
you stored it as a QPoint so you must read it back as a QPoint! :)
Code:
QVector<QPoint> points;
QVector<qint32> x, y;
in >> p;
x << p.x();
y << p.y();
Re: Read binary file into 2 QVectors
Thanks, i did it your way , except, last two lines (it didn't work for me :S):
Code:
.......
for(int i=0;i<p.size();i++){
x.append(p.at(i).x());
y.append(p.at(i).y());
}
..but now this looks primitive and i think it can be done a lot shorter and faster..
Re: Read binary file into 2 QVectors
Code:
QVector<qint32> x, y;
while (!in.atEnd())
{
in >> p;
x.push_back(p.x());
y.push_back(p.y());
}
Re: Read binary file into 2 QVectors
If you already have the vector of points in memory why aren't you just using them as-is? Seems you are writing a file only to transform the data.
Re: Read binary file into 2 QVectors
Firstly, I parse xml file and after calculating points for plotting graph, i save those points to binary file so it can be easier to load them later without parsing xml again.
Problem is, that data can be added to plotting function as two vectors of doubles, one vector for x, other for y coordinate.
This is the function i use: void QCPGraph::addData ( const QVector< double > & keys, const QVector< double > & values ) from QCustomPlot library
Re: Read binary file into 2 QVectors
so parse the file into memory in convenient format - and KEEP it in memory. File access is very slow!