I have a CSV file and I want to delete a specific line in it. The way I want to do this is by reading the file line by line and checking if the first element of the line is equal to a variable. If it's equal => remove the line.
// get the project name from the combo box
QVariant projectName = ui->projectCombo->currentText();
QFile projectFile(CSV File);
// checks if file is valid and can open
if(!projectFile.open(QIODevice::ReadOnly)) {
cout << "Error: can't copen file" << endl;
} else {
cout << "File open successfully" << endl;
}
QTextStream in(&projectFile);
while(!in.atEnd()) {
QString line = in.readLine();
// Get the first element from line
QString firstElement = line.split(',').first();
// Check if firstElement is equal to the variable projectName
if (firstElement == projectName.toString()) {
// Delete current line
}
}
I'm doing this because I want to update specific data on that line. I know Qt, let alone c++, can't do this. So I thought I get the line from the file, put it in a data structure, delete it from the file, edit the desired data inside the data structure and then append to file the changed data from the data structure