Results 1 to 7 of 7

Thread: Read binary file into 2 QVectors

  1. #1
    Join Date
    Apr 2012
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Read binary file into 2 QVectors

    Hello, I have one question about reading binary files.

    I wrote data like this:
    Qt Code:
    1. QVector<QPoint> p;
    2.  
    3. QFile binfile(path);
    4. binfile.open(QIODevice::WriteOnly);
    5. QDataStream out(&binfile);
    6.  
    7. out << p;
    To copy to clipboard, switch view to plain text mode 


    ..and tried reading it this way:
    Qt Code:
    1. QFile bin(fileName);
    2. bin.open(QIODevice::ReadOnly);
    3. QDataStream in(&bin);
    4.  
    5.  
    6. QVector<qint32> x, y;
    7.  
    8. in >> x >>y;
    To copy to clipboard, switch view to plain text mode 


    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..
    Last edited by juracist; 17th May 2012 at 19:41.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read binary file into 2 QVectors

    you stored it as a QPoint so you must read it back as a QPoint!

    Qt Code:
    1. QFile bin(fileName);
    2. bin.open(QIODevice::ReadOnly);
    3. QDataStream in(&bin);
    4.  
    5. QVector<QPoint> points;
    6. QVector<qint32> x, y;
    7.  
    8. in >> p;
    9. x << p.x();
    10. y << p.y();
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Apr 2012
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read binary file into 2 QVectors

    Thanks, i did it your way , except, last two lines (it didn't work for me :S):
    Qt Code:
    1. .......
    2. for(int i=0;i<p.size();i++){
    3. x.append(p.at(i).x());
    4. y.append(p.at(i).y());
    5. }
    To copy to clipboard, switch view to plain text mode 

    ..but now this looks primitive and i think it can be done a lot shorter and faster..

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read binary file into 2 QVectors

    Qt Code:
    1. QFile bin(fileName);
    2. bin.open(QIODevice::ReadOnly);
    3. QDataStream in(&bin);
    4.  
    5. QVector<qint32> x, y;
    6.  
    7. while (!in.atEnd())
    8. {
    9. in >> p;
    10. x.push_back(p.x());
    11. y.push_back(p.y());
    12. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  6. #6
    Join Date
    Apr 2012
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Read binary file
    By jaca in forum Qt Programming
    Replies: 9
    Last Post: 28th March 2012, 09:38
  2. Max and min from QVector<QPoint>
    By awpitt13 in forum Newbie
    Replies: 4
    Last Post: 14th February 2012, 02:18
  3. Read a Byte from Binary File
    By umulingu in forum Qt Programming
    Replies: 1
    Last Post: 3rd March 2010, 07:20
  4. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 4th April 2009, 00:50
  5. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 14th June 2008, 00:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.