Qt 5 Read a Text File With Delimiters

Qt 5 Read a Text File With Delimiters

The following example reads a text file of a maximum of 3000 lines.
I use arrays of integers for ease of example, but for larger files
that may vary in size, vector types would be appropriate, but add
extra complexity in accessing and manipulating vector vales for
multidimentional vectors. Each line contains a date, followed by
6 numbers separated by a varying number of spaces. The delimiter
in the example is spaces, but you can use any delimiter , such as
commas, or tabs (\t). The example looks for an input file, then if
it doesn’t find it, it tries to copy it from the application startup
folder. This adds extra code to the example, but demonstrates a real
world example of how to handle input files. If you find this extra
code distracting leave a comment and I’ll remove it.

Myclass.h:

 

#include <QString>
#include <QLineEdit>


class Myclass
{

public:

QString linedate [3000];
int numbers [3000][6];
int linestotal;
bool inputfilefound;

public:

void readTextFile(void);
void copyFile(QString srcPath, QString destPath);

};


 

Myclass.cpp:

 

#include "Myclass.h";
#include <QStandardPaths>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include <QDir>
#include <QCoreApplication>


void Myclass::readTextFile(void) {

int i,j;
bool ok;

QString Temp;
QStringList Temp2;
QMessageBox msgBox;
QString path, path2;
QString filetocopy = "myfile.txt";

// Read the numbers

inputfilefound = true;

path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
path += "/My Documents/myfolder";

// If folder doesn't exist, create it

QDir dir(path);
if (!dir.exists())

    dir.mkdir(path);

path += "/myfile.txt";

QFile inFile (path);

// Open file if it exists

if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text)) {

// If file doesn't exist in folder, copy it from the applications startup folder

    path2 = QCoreApplication::applicationDirPath() + "/" + filetocopy;

    copyFile (path2, path);

    // If source file doesn't exist, exit procedure

    if (!inputfilefound) {

        return;

    }

    // If can't open file in folder, exit

    if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text)) {

        msgBox.setText ("Unable to open input file");
        msgBox.exec();

        return ;

    }

}

QTextStream in(&inFile);

linestotal = 0;

while (!in.atEnd()) {

    Temp = in.readLine();

    if (Temp.isEmpty()) break;

    // Go through and split string and remove delimeters to leave just items

    // Delimiter is a sapce (" "), but can be anything, such as comma (",") or tab ("\t")

    Temp2 = Temp.split(" ");

    int LastNonEmpty = -1;

    for (i = 0; i < Temp2.size(); i++) {

        if (Temp2[i] != "") {

            LastNonEmpty += 1;

            Temp2[LastNonEmpty] = Temp2[i];

        }

    }

    linedate [linestotal] = Temp2 [0];

    j = 1;

    for (i = 0; i <= 5; i++) {

        Temp = Temp2 [j];

        numbers [linestotal][i] = Temp.toInt(&ok,10);

        j ++;
    }

    linestotal++;
}

inFile.close();

linestotal--;

}

void Myclass::copyFile(QString srcPath, QString destPath)
{
    QMessageBox msgBox;
    QFile srcFile(srcPath);
    QFile destFile(destPath);

    inputfilefound = true;

    if (srcFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        if (destFile.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            QTextStream srcStream(&srcFile);
            QTextStream destStream(&destFile);

            while (!srcStream.atEnd())
            {
            // :NOTE: QTextStream::readLine() strips the trailing newline
            // so we are putting it back with the "\n". This may cause
            // newlines to change from "\n" to \r or similar problems depending
            // on platform. Check out QIODevice::readLine() if that is a problem

                destStream << srcStream.readLine() << endl;
            }

           destFile.close();

        }

    srcFile.close();

    }

    else {

         msgBox.setText ("Unable to find input file");
         msgBox.exec();

         inputfilefound = false;

    }

}

 

Advertisements