Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Writing to a CSV file type

    General and Desktop
    3
    4
    3023
    Loading More Posts
    • 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_DAntonio
      Steven_DAntonio last edited by

      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

      kshegunov 1 Reply Last reply Reply Quote 0
      • ?
        A Former User last edited by

        @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 Reply Quote 1
        • kshegunov
          kshegunov Moderators @Steven_DAntonio last edited by

          @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 Reply Quote 0
          • Steven_DAntonio
            Steven_DAntonio last edited by

            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 Reply Quote 0
            • First post
              Last post