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. C++ how to open a csv file and save as new file?
Forum Updated to NodeBB v4.3 + New Features

C++ how to open a csv file and save as new file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.1k Views 1 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.
  • M Offline
    M Offline
    mangekoyu
    wrote on 5 Aug 2022, 08:42 last edited by
    #1

    I have a template CSV file. I want open it and write some datas. But template file should not save. I need save as new file and close template file without save. How can do this?

    Here is function I use .

    void MainWindow::exportArraysToCSV(QStringList labelList, QList<QList<double>> dataColums, QChar sep)
    {
        QString filters("CSV files (*.csv);;All files (*.*)");
        QString defaultFilter("CSV files (*.csv)");
        QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
                                                        filters, &defaultFilter);
        QFile file(fileName);
    
        if (file.open(QFile::WriteOnly | QFile::Append))
        {
            QTextStream data(&file);
            QStringList strList ;
           
            foreach (auto label, labelList)
            {
                if (label.length() > 0)
                    strList.append(label);
                else
                    strList.append("");
            }
    
            data << strList.join(";") << "\n";
    
            int maxRowCount = 0;
            foreach (auto column, dataColums)
                maxRowCount = qMax(maxRowCount, column.count());
               qDebug() << maxRowCount;
            for (int i = 0; i < maxRowCount; ++i) // rows
            {
                strList.clear();
                for (int j = 0; j < 2; ++j) // columns
                {
                    if (i < dataColums[j].count())
                        {
                        strList.append(QString::number(dataColums[j][i], 'f'));
    
    
                    }
    
                    else
                        strList.append("\"\"");
                }
                data << strList.join(";") << "\n";
    
            }
            file.close();
        }
    }
    
    J 1 Reply Last reply 5 Aug 2022, 09:42
    0
    • M mangekoyu
      5 Aug 2022, 08:42

      I have a template CSV file. I want open it and write some datas. But template file should not save. I need save as new file and close template file without save. How can do this?

      Here is function I use .

      void MainWindow::exportArraysToCSV(QStringList labelList, QList<QList<double>> dataColums, QChar sep)
      {
          QString filters("CSV files (*.csv);;All files (*.*)");
          QString defaultFilter("CSV files (*.csv)");
          QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
                                                          filters, &defaultFilter);
          QFile file(fileName);
      
          if (file.open(QFile::WriteOnly | QFile::Append))
          {
              QTextStream data(&file);
              QStringList strList ;
             
              foreach (auto label, labelList)
              {
                  if (label.length() > 0)
                      strList.append(label);
                  else
                      strList.append("");
              }
      
              data << strList.join(";") << "\n";
      
              int maxRowCount = 0;
              foreach (auto column, dataColums)
                  maxRowCount = qMax(maxRowCount, column.count());
                 qDebug() << maxRowCount;
              for (int i = 0; i < maxRowCount; ++i) // rows
              {
                  strList.clear();
                  for (int j = 0; j < 2; ++j) // columns
                  {
                      if (i < dataColums[j].count())
                          {
                          strList.append(QString::number(dataColums[j][i], 'f'));
      
      
                      }
      
                      else
                          strList.append("\"\"");
                  }
                  data << strList.join(";") << "\n";
      
              }
              file.close();
          }
      }
      
      J Offline
      J Offline
      JonB
      wrote on 5 Aug 2022, 09:42 last edited by JonB 8 May 2022, 09:44
      #2

      @mangekoyu said in C++ how to open a csv file and save as new file?:

      I need save as new file and close template file without save.

      So do exactly that! If you need to read an existing file, or a "template" file, open that read-only. Quite separately, open new file for write to save whatever data as a new file.

      Your code above opens file for append. User chooses save file, he might select existing file/template file, though he will get warning if he does so that it will be overwritten --- or in this case, only appended to. Don't know if that is what you intend.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mangekoyu
        wrote on 5 Aug 2022, 10:27 last edited by
        #3

        My intention is to write the data to the template file exactly. Next, I need to save the template file where I wrote my data. When I save as a new file, I have to close my original template file without saving anything. Because I will reuse my template file.

        J M 2 Replies Last reply 5 Aug 2022, 11:06
        0
        • M mangekoyu
          5 Aug 2022, 10:27

          My intention is to write the data to the template file exactly. Next, I need to save the template file where I wrote my data. When I save as a new file, I have to close my original template file without saving anything. Because I will reuse my template file.

          J Offline
          J Offline
          JonB
          wrote on 5 Aug 2022, 11:06 last edited by JonB 8 May 2022, 11:29
          #4

          @mangekoyu
          I am sorry, I really don't follow what you are saying/asking here!

          Like I said, if you want to "save as new file and close template file without save" then do precisely that. I do not understand what your problem/question actually is.

          @JonB said in C++ how to open a csv file and save as new file?:

          If you need to read an existing file, or a "template" file, open that read-only. Quite separately, open new file for write to save whatever data as a new file.

          P.S.
          QFile::copy() works this way, is that all you want? It has nothing to do with "template" or "CSV" files....

          1 Reply Last reply
          2
          • M mangekoyu
            5 Aug 2022, 10:27

            My intention is to write the data to the template file exactly. Next, I need to save the template file where I wrote my data. When I save as a new file, I have to close my original template file without saving anything. Because I will reuse my template file.

            M Offline
            M Offline
            mpergand
            wrote on 5 Aug 2022, 11:44 last edited by
            #5

            @mangekoyu said in C++ how to open a csv file and save as new file?:

            My intention is to write the data to the template file exactly. Next, I need to save the template file where I wrote my data. When I save as a new file, I have to close my original template file without saving anything. Because I will reuse my template file.

            If I follow you correctly, you need to define a extension for your template files.

            QString filters("CSV files (*.csv);; Template CSV files (*.tcsv);; All files (*.*)");
            

            This way you can select the type of file you want to save in the file dialog.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mangekoyu
              wrote on 5 Aug 2022, 13:18 last edited by
              #6

              @mpergand said in C++ how to open a csv file and save as new file?:

              CSV file

              I guess I didn't convey my problem properly. I'm sorry for that. I will open my template file to save my data. Let's call it temp.csv. I will then write the data to my template file. I don't want to export my template file after my data is written. I want to save a copy of the temp.csv file, for example data.csv. when i save my data.csv file. It will close without saving anything to my temp.csv file. and when I want to export again, my template file will be opened and the same process will be applied. my purpose here. I have a set of charts and functions in my csv file. When exporting csv files, I cannot add things like graphics due to their nature. because it will be a gradual csv that I have prepared by hand. For each export I will write the data I will use it into. I'll save it as a copy and close it back without saving anything to my original file so I have a template ready for my other uses. I hope it was more descriptive.

              M 1 Reply Last reply 5 Aug 2022, 13:33
              0
              • M mangekoyu
                5 Aug 2022, 13:18

                @mpergand said in C++ how to open a csv file and save as new file?:

                CSV file

                I guess I didn't convey my problem properly. I'm sorry for that. I will open my template file to save my data. Let's call it temp.csv. I will then write the data to my template file. I don't want to export my template file after my data is written. I want to save a copy of the temp.csv file, for example data.csv. when i save my data.csv file. It will close without saving anything to my temp.csv file. and when I want to export again, my template file will be opened and the same process will be applied. my purpose here. I have a set of charts and functions in my csv file. When exporting csv files, I cannot add things like graphics due to their nature. because it will be a gradual csv that I have prepared by hand. For each export I will write the data I will use it into. I'll save it as a copy and close it back without saving anything to my original file so I have a template ready for my other uses. I hope it was more descriptive.

                M Offline
                M Offline
                mpergand
                wrote on 5 Aug 2022, 13:33 last edited by
                #7

                @mangekoyu said in C++ how to open a csv file and save as new file?:

                I will open my template file to save my data.

                There are two ways to understand this phrase,

                1. you open then load the file in memory
                2. you open the temp file and write directly new data in it.

                Seems you do the second one and of course you modified the original temp file.
                What you have to do is to duplicate the original file first.
                Seems what @JonB has suggested earlier.

                1 Reply Last reply
                2

                1/7

                5 Aug 2022, 08:42

                • Login

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