Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing to a CSV file type
Forum Updated to NodeBB v4.3 + New Features

Writing to a CSV file type

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Steven_DAntonioS Offline
    Steven_DAntonioS Offline
    Steven_DAntonio
    wrote on last edited by
    #1

    HI,

    I'm trying to write a set of data to a CSV file in Qt 5.6 on a windows machine.

    So far I have

    // Save as a CSV file
    
    QString species;
    
    species = ui->tBSpecies->text();
    

    QFile file("./file.csv");
    if (file.open(QFile::WriteOnly|QFile::Truncate))
    {
    QTextStream stream(&file);
    stream << species << "\t" << gType << "\t" << species << "\t" << tSLen << "\t" << tSWidth << "\t" << tSThick << "\t"
    << tSMass << "\t" << den << "\t" << fLong << "\t" << fCross << "\t" << fDiag << "\t"
    << eLong << "\t" << eCross << "\t" << eDiag << "\t" << eG << "\t" << targetThick << "\n"; // this writes first line with two columns

    file.close();

    Most of the variables are doubles and come in from another function (species and gType are the only strings).

    The part I am having trouble with is looking at the CSV file to see if it already contains data and placing the new data in the first open row so that i do not overwrite existing data.

    Anything that points me in the right direction would be great.

    Thank you
    Steven

    kshegunovK 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      @Steven_DAntonio Hi!

      if it already contains data and placing the new data in the first open row

      Then why do you use QIODevice::Truncate? I think QIODevice::Append would be the right choice in this case.

      1 Reply Last reply
      1
      • Steven_DAntonioS Steven_DAntonio

        HI,

        I'm trying to write a set of data to a CSV file in Qt 5.6 on a windows machine.

        So far I have

        // Save as a CSV file
        
        QString species;
        
        species = ui->tBSpecies->text();
        

        QFile file("./file.csv");
        if (file.open(QFile::WriteOnly|QFile::Truncate))
        {
        QTextStream stream(&file);
        stream << species << "\t" << gType << "\t" << species << "\t" << tSLen << "\t" << tSWidth << "\t" << tSThick << "\t"
        << tSMass << "\t" << den << "\t" << fLong << "\t" << fCross << "\t" << fDiag << "\t"
        << eLong << "\t" << eCross << "\t" << eDiag << "\t" << eG << "\t" << targetThick << "\n"; // this writes first line with two columns

        file.close();

        Most of the variables are doubles and come in from another function (species and gType are the only strings).

        The part I am having trouble with is looking at the CSV file to see if it already contains data and placing the new data in the first open row so that i do not overwrite existing data.

        Anything that points me in the right direction would be great.

        Thank you
        Steven

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Steven_DAntonio said:

        The part I am having trouble with is looking at the CSV file to see if it already contains data and placing the new data in the first open row so that i do not overwrite existing data.

        Are you trying to update rows that are already written? If so, you can't. CSV is a text file, so this won't work. If you only want to append, see @Wieland's suggestion.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • Steven_DAntonioS Offline
          Steven_DAntonioS Offline
          Steven_DAntonio
          wrote on last edited by
          #4

          HI,

          Thank you for the replies, it looks like the append function is just what I may be looking for. I'm not trying to change any values, in fact changing anything that is written previously is just what I want to avoid. What I'm looking to do is to keep the order (species (the first variable) is always saved to column 1 of the first available empty row, gType to the second etc.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved