C++ how to open a csv file and save as new file?
-
wrote on 5 Aug 2022, 08:42 last edited by
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(); } }
-
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(); } }
wrote on 5 Aug 2022, 09:42 last edited by JonB 8 May 2022, 09:44@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.
-
wrote on 5 Aug 2022, 10:27 last edited by
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.
-
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.
wrote on 5 Aug 2022, 11:06 last edited by JonB 8 May 2022, 11:29@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.... -
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.
wrote on 5 Aug 2022, 11:44 last edited by@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.
-
wrote on 5 Aug 2022, 13:18 last edited by
@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.
-
@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.
wrote on 5 Aug 2022, 13:33 last edited by@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,
- you open then load the file in memory
- 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/7