2

I'm trying to write a program which reads in a text file containing several rows of 512 elements. These are numbers separated by a tab, the first line of the file contains general info about the file itself. Here's what it looks like:

512 Measurement taken on: Thu 12. Jun 18:35:44 2014 Comments: nil
2.4155 1.60983 1.08339 0 2.13321 0 0.848402 0 0.747692 0 0.146487 0 1.98062 0 0.846876 0 1.87991 0.117494 0 0 0 0 1.6907 0 0 0 0.0671397 0.256352 1.33974 1.4313 1.17494 0 0 0 1.83566 0 2.54826 0 0 0 0 0 1.80819 0 0 0 0 0 4.78523 0 1.99283 0 2.63676 0 2.19272 0 0.962844 0.256352 0.762951 0.581369 0 0 0 0.689708 1.38552 2.38193 1.11391 3.22118 0.712596 0 0.508125 0 0 0.842298 0 0.794995 0.967422 0.820935 0.0534066 2.67338 0

etc etc.

Each of the 512 columns represents a separate data stream and the rows are sampled over time. The program calculates the integral of the data so that it spits out a vector with 512 elements, each element is the sum of all the data in the respective column.

The integral part works fine, i can print the output of the vector using qDebug, but when I try and write the vector to a text file I get an error.

Here is the code I'm using:

void MainWindow::on_pushButton_clicked()

QVector<double> SingleLineData;
SingleLineData.resize(512);
QString test;
QString inputfile = QFileDialog::getOpenFileName(
            this,
            tr("Open File"),
            "/Users",
            "All files (*.*)"
            );

if(inputfile != ""){
QFile file(inputfile);


if(!file.open(QFile::ReadOnly)){
   }
QTextStream in(&file);


    double buffer;

    while(!file.atEnd()){
        in.readLine();
        for(int i=0; i<512; i++){
            in >> buffer;
            SingleLineData[i]+=buffer;
        }
    }

}
    qDebug() << SingleLineData;

// ************* file output **************************************************

QString filename = "/Users/Mitch/Desktop/integral.txt";
QFile fileout(filename);
if (fileout.open(QFile::ReadWrite | QFile::Text)){
}
     QTextStream out(&fileout);
     out << SingleLineData;
     fileout.close();

}

And the error is receive says:

error: invalid operands to binary expression ('QTextStream' and 'QVector') and candidate function not viable: no known conversion from 'QVector' to 'const void *' for 1st argument; take the address of the argument with & QTextStream &operator<<(const void *ptr);

Any help would be much appreciated! :)

Mitchell D
  • 465
  • 8
  • 24

2 Answers2

2

This is the iterator method. The operator for QTextStream is not overloaded to accept a vector. However, it can accept a double.

QString filename = "/Users/Mitch/Desktop/integral.txt";
QFile fileout(filename);
if (fileout.open(QFile::ReadWrite | QFile::Text)){
 QTextStream out(&fileout);
 for (Qvector<double>::iterator iter = SingleLineData.begin(); iter != SingleLineData.end(); iter++){
     out << *iter;
 }
 fileout.close();
}
Michael LeVan
  • 528
  • 2
  • 8
  • 21
  • Thanks melevan, that seems to have solved the error. The output is now is hex I think, any way to convert it to plain text/regular numbers? Here is a sample of the output: 0x7fd09306a0180x7fd09306a0200x7fd09306a0280x7fd09306a0300x7fd09306a0380x7fd09306a0400x7fd09306a0480x7fd09306a0500x7fd09306a0580x7fd09306a0600x7fd09306a0680x7fd09306a0700x7fd09306a0780x7fd09306a0800x7fd09306a0880x7fd09306a0900x7fd09306a0980x7fd09306a0a00x7fd09306a0a80x7fd09306a0b00x7fd09306a0b80x7fd09306a0c00x – Mitchell D Feb 27 '15 at 07:50
  • So I experimented with removing the * before iter, when its just plain iter then the data is printed incorrectly in hex, but with the *iter its fine. Being fairly new to C++ I'm not really understanding why using * makes it work, something to do with pointers? So now its working printing out the numbers, but there is no delimiter, anyway to add a comma and a space like 122112, 12121435, 242425235 etc etc – Mitchell D Feb 27 '15 at 07:56
  • Ok it was as easy as adding << ", " to out << *iter Still wondering why a pointer is needed for iter though? – Mitchell D Feb 27 '15 at 08:03
  • @MitchellD It's called dereferencing. You want to print the item the iterator points to, not the iterator itself. You can check the docs about [STL-style iterators](http://qt-project.org/doc/qt-4.8/containers.html#stl-style-iterators). You should also read about [pointers](http://www.cplusplus.com/doc/tutorial/pointers/). – thuga Feb 27 '15 at 08:21
  • @thuga ok that makes sense. Thanks a lot, I'll be sure to check out the documentation :) – Mitchell D Feb 27 '15 at 09:14
1

Just as the error message says, there's no QTextStream overload that takes a QVector<double>, but there is for double. So just iterate through the elements and steam them individually.

cmannett85
  • 21,725
  • 8
  • 76
  • 119