1

I am currently working on a Qt application which works with CSV files. I have implemented a method whose only objective is to display content on the console (for the moment).

Here is the relevant code:

Class AppMainWindow, method loadCsv

void AppMainWindow::loadCsv() {
    cout << "Sélection du fichier" << endl;

    QString fileName = QFileDialog::getOpenFileName(NULL, "Ouvrir un fichier", QString(), "Tableau CSV (*.csv)");
    if (fileName != NULL && !fileName.isEmpty()) {
        cout << "Ouverture du fichier \"" << fileName.toStdString() << "\"" << endl;
        string name = fileName.toStdString();
        vector<vector<string>> data = CsvReader::readCsv(&name);
    } else
        cout << "Pas de fichier sélectionné" << endl;
}

CsvReader.h

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

namespace CsvReader {
    vector< vector<string> > readCsv(string *fileStream);
}

CsvReader.cpp

#include "src/com/alten/utility/CsvReader.h"

vector<vector<string>> CsvReader::readCsv(string *fileName) {
    vector < vector<string> > data;
    string line;

    ifstream fileStream(*fileName);
    while (getline(fileStream, line)) {
        vector < string > row;
        string element;

        cout << "Ligne :" << line << endl;

        stringstream lineStream(line);
        while (getline(lineStream, element, ',')) {
            cout << "Item :" << element << endl;
            row.push_back(element);
        }

        data.push_back(row);
    }

    fileStream.close();
}

When I load any CSV file, loadCsv executes normally until the end of the if statement, where the Qt application suddenly freezes.

There is not that problem when I remove my if statement:

void AppMainWindow::loadCsv() {
    cout << "Sélection du fichier" << endl;

    QString fileName = QFileDialog::getOpenFileName(NULL, "Ouvrir un fichier", QString(), "Tableau CSV (*.csv)");
    cout << "Ouverture du fichier \"" << fileName.toStdString() << "\"" << endl;
    string name = fileName.toStdString();
    vector<vector<string>> data = CsvReader::readCsv(&name);
}

I am not Cpp-fluent, could anyone please explain to me where do the error come from?

NB: I compile my code with cmake.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0009laH
  • 1,960
  • 13
  • 27
  • OT: `string *fileName` why? const std::string & fileName would be better – drescherjm May 06 '20 at 13:54
  • 1
    You probably have to use a debugger to track this down. If you are using qt-creator you should have one. – drescherjm May 06 '20 at 13:57
  • @drescherjm I thought passing fileName as a value was useless but I am not yet familiar with pointers. I am working with Eclipse CDT4, all my interface is handmade-implemented :) – 0009laH May 06 '20 at 13:57
  • What I meant was change `CsvReader::readCsv(string *fileName)` to `CsvReader::readCsv(const std::string & fileName )` and stop using the pointer to a string. Instead use a reference which is preferred in `c++`. This is not going to fix your problem as I don't even see it that is why I mentioned debugging. – drescherjm May 06 '20 at 14:03
  • @drescherjm I have tried your proposition but I have the same behaviour. I'll follow your suggestion, maybe there will be some changes – 0009laH May 06 '20 at 14:08
  • I expected that. If you don't have a debugger you will have to add some cout statements. – drescherjm May 06 '20 at 14:10
  • @drescherjm OK I'll do my best to find it, thanks for your help :) – 0009laH May 06 '20 at 14:24
  • @melk The URL doesn't work :/ – 0009laH May 06 '20 at 14:26
  • Repostng link, couldn't edit my previous comment for some reaseon. https://stackoverflow.com/questions/7855226/use-getline-without-setting-failbit – melk May 06 '20 at 14:33

1 Answers1

1

you should:

  1. open the file -> checking if something failed
  2. read all line by line

  3. skip the empty parts push every row in a vector, list, array or whatever...

--

bool parseCsvFileQt(const QString& filename, QVector<QString>& row)
{
    QFile file(filename);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() << file.errorString();
        return false;
    }
        QString line = file.readAll();
        line = line.remove("\r");
        QStringList arrayOfLines = line.split("\n", QString::SkipEmptyParts);

        for(int i = 1; i < arrayOfLines.size(); i++)
        {
            row.push_back(arrayOfLines[i]);
        }
    return true;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97