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.