areadcsv.cpp 948 B

123456789101112131415161718192021222324252627282930313233
  1. #include "areadcsv.h"
  2. /*!
  3. * \brief aReadCSV reads from a CSV file
  4. * \param filename - QString to csv file.
  5. * \return QVector<QStringList> of the CSV data, where each QStringList is one column of the input file
  6. */
  7. QVector<QStringList> aReadCsv(QString filename)
  8. {
  9. QFile csvfile(filename);
  10. csvfile.open(QIODevice::ReadOnly);
  11. QTextStream stream(&csvfile);
  12. QVector<QStringList> values;
  13. //Read CSV headers and create QStringLists accordingly
  14. QString line = stream.readLine();
  15. auto items = line.split(",");
  16. for (int i = 0; i < items.length(); i++) {
  17. QStringList list;
  18. list.append(items[i]);
  19. values.append(list);
  20. }
  21. //Fill QStringLists with data
  22. while (!stream.atEnd()) {
  23. QString line = stream.readLine();
  24. auto items = line.split(",");
  25. for (int i = 0; i < values.length(); i++) {
  26. values[i].append(items[i]);
  27. }
  28. }
  29. return values;
  30. }