0

Im creating a program that reads from a text file and plots the integers. Here is what I have so far

#include "realtime.h"
#include "ui_realtime.h"
#include <QFile>
#include<QIODevice>
Realtime::Realtime(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Realtime)
{
    ui->setupUi(this);
    int size = 1000;
    QVector<double> x(size), y(size);
    for (int i=0; i<size; i++)
    {
        x[i]= i;
        y[i]= i ;
    }
    ui->plot->addGraph();
    ui->plot->graph(0)->setData(x,y);
    ui->plot->xAxis->setRange(0,10);
    ui->plot->yAxis->setRange(0,10);
}

Realtime::~Realtime()
{
    delete ui;
} 
int main()
{
    std::vector<int>ints;
    QFile file("2dplotarray.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))

        while (!file.atEnd())
        {
            QByteArray line = file.readLine();
            QDataStream ds(line);
            int int_in_line = 0;
            ds >> int_in_line;
            ints.push_back(int_in_line);
        }
    return 0    ;
}

Ignore the current x and y values, that was me testing the plotting features. How do i put the text file into the y values of my plot? The text file looks like this(NOT CODE just the best way to display it)

    1
    2
    3
    4
    etc...
  • Off topic: I don't know `QFile` from a hole in the ground, but I recommend reading the documentation carefully before trusting that this will work: `while (!file.atEnd())`. Most file-reading implementations do not set the end of file flag until after a read attempt has been made that reaches the end of file. Testing for end of file before performing the read that finds the end of file, and thus attempting to use data that was not read, is a common pitfall. Read more here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Mar 03 '16 at 18:22
  • Wait a sec. Is your question basically, "How do I get `ints` from `main` to `Realtime::Realtime`?" – user4581301 Mar 03 '16 at 18:33

1 Answers1

0

Unless the unstated point is to use the QT classes, I'd just use an input file stream if I were you. Here is what some code might look like to read in those values.

#include <fstream>
#include <string>

int main()
{
    std::vector<int> vecValues;
    std::ifstream file("yourfilename.txt");
    if (file.is_open())
    {    
        //if you can anticipate that the only values will be integers
        //then this will work just fine
        int value;
        while (file >> value)
        {
            vecValues.push_back(value);
        }

        //or you could modify the following if you expected non-numbers
        //in your file and need more control over handling them
        //std::string fileLine;
        //while ( std::getline(file, fileLine) )
        //{
        //  vecValues.push_back(std::atoi(fileLine.c_str()));
        //}

        file.close();
    }

    return 0;
}
Jacob Statnekov
  • 235
  • 4
  • 11
  • Thank you, quick question because im somewhat new at this, vecValues is your variable name correct? And basically I would want to set the y values = to vecValues right? – Brandon McCoy Mar 06 '16 at 18:14
  • @BrandonMcCoy You can't copy a std::vector into a QVector directly because QVector doesn't have the right assignment operator, but there are very simple ways of getting what you want. If you replace the std::vector in your code with a QVector then you can copy your "ints" variable into "y". Notice that the type changes from int to double for "ints" because Y is a vector of doubles. You should read about what methods QVector offers since it is very similar to std::vector [link] (http://doc.qt.io/qt-4.8/qvector.html) ). My advice is to not use QT until you understand the C++ basics. – Jacob Statnekov Mar 06 '16 at 19:50