Create .csv file in C++ in qt
Asked Answered
M

3

9

I want to create an csv file using c++, using Qt for application and UI framework. Is there's library for csv file.

Middelburg answered 2/4, 2014 at 5:45 Comment(3)
@RobbieE Of course if it's to be usable beyond a highschool homework assignment, it's not exactly so trivial anymore...Macknair
C CSV Parser: sourceforge.net/projects/cccsvparser C CSV Writer: sourceforge.net/projects/cccsvwriterCorrespond
So what if it is a trivial question? Everyone start learning from 0. If you don't want to help OP, then just bugger off.Catenoid
D
4

Try qtcsv library for reading and writing csv-files. Example:

#include <QList>
#include <QStringList>
#include <QDir>
#include <QDebug>

#include "qtcsv/stringdata.h"
#include "qtcsv/reader.h"
#include "qtcsv/writer.h"

int main()
{
    // prepare data that you want to save to csv-file
    QStringList strList;
    strList << "one" << "two" << "three";

    QtCSV::StringData strData;
    strData.addRow(strList);
    strData.addEmptyRow();
    strData << strList << "this is the last row";

    // write to file
    QString filePath = QDir::currentPath() + "/test.csv";
    QtCSV::Writer::write(filePath, strData);

    // read data from file
    QList<QStringList> readData = QtCSV::Reader::readToList(filePath);
    for ( int i = 0; i < readData.size(); ++i )
    {
        qDebug() << readData.at(i).join(",");
    }

    return 0;
}

I tried to make it small and easy-to-use. See Readme file for library documentation and other code examples.

Disqualify answered 22/5, 2015 at 8:35 Comment(4)
So I tried this code: QStringList strList; strList << "one" << "two" << "three" << "with,comma"; and got one,two,three,with,comma in file instead of one,two,three,"with,comma", am I use it correctly or lib can't handle such simple thing?Boehmenism
@Chernobyl Well, it seems that this is a mistake in library. Will try to fix it. As a workaround try to use different separator symbol (for example, use semicolon ";").Disqualify
Ok, notify me, if will be fixed, upvoted because lib is quite interesting, but this bug makes it almost useless, will be better with good fix.Boehmenism
@Chernobyl checkout latest version on GitHubDisqualify
A
1

Just writing CSV? Although google may reveal some CSV libraries, the probable reason why you have not found any is because it is so darn trivial. Remember CSV is just Comma Separated Values.

To implement it use any means to write a text file (std::ofstream, QFile, QTextStream) and do something along the lines of:

foreach record
{
  foreach value in record
  {
    write "\"" + escape(value) + "\""
    if not last value in record
    {
      write ","
    }        
  }
  write "\n"
}


escape (value)
{
  replace each "\"" with "\"\""
}

Note that you can write the values without quotes if they do not contain any separators (,). Also note you can use different separators, for example the semi-colon is commonly used.

Amnesty answered 2/4, 2014 at 5:57 Comment(8)
Given that you have had a serious bug in the escape function, I dare say it's not so trivial :(Macknair
Granted that was an oversight... But it still remains trivial.Amnesty
OK, so what text encoding is that CSV file written in? :) When it's all said and done, for a generic, robust implementation you need a tested and field proven library. Home-spun approaches work for only the simplest data.Macknair
your write "\n" is in the wrong spot, move it down a lineVirgilvirgilia
Thank you. Boy, today is day of the logic flaws. This shows that you should also check the "simple" and "obvious" things.Amnesty
Indeed, writing CSV is only trivial as long as it is plain numbers. After that, things start to get complicated fast. And to have any kind of confidence in the implementation, it also needs tests to cover all the corner cases, which are many, not to mention manual testing to see other apps also load the corner cases correctly. An existing library would allow pretending someone else has done this ;)Ninnetta
CSV files escape quotes by doubling them, not by preceding them with a backslash. tools.ietf.org/html/rfc4180Merca
@MichaelBurr Thank you for the correction, I got confused from my parser implementation, it accepts both variations. On the reading side you need accept all the variation that are in the wild and these are meany. Common variations are the separator (,, ;, \t) and escape sequences ("", \"), including the C-style escapes (\n, \r \t, \a, etc.) or newlines in within sting (which is allowed). Reading CSV is would always encourage to use a lib.Amnesty
B
1

You could basically look into libqxt.

Using QxtCsvModel

The QxtCsvModel [libqxt.bitbucket.org] class provides a QAbstractTableModel [qt-project.org] for CSV Files. This is perhaps the easiest way possible to read and write csv files without having to parse the csv format to something qt can understand. It’s as simple as using one line of code, for example the following reads the csv file:

csvmodel->setSource(fileName);
Barbi answered 5/4, 2014 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.