How can I delete first line of text file before write
-
I want to delete first line fo text file before write and after deleting write in first line how can I do that there is my code;
void SecondWindow::on_pushButton_8_clicked() { QFile file("C:/Users/umutc/Desktop/Test/text/test.txt"); //I need delete first line here before write if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) return; { QTextStream out(&file); out << ui->newPassBox->text(); } }
-
Use QTextStream::readLine() and don't write out the first line.
-
I want to delete first line fo text file before write and after deleting write in first line how can I do that there is my code;
void SecondWindow::on_pushButton_8_clicked() { QFile file("C:/Users/umutc/Desktop/Test/text/test.txt"); //I need delete first line here before write if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) return; { QTextStream out(&file); out << ui->newPassBox->text(); } }
@TheCeylann You will need to write new content into a temporary file, then replace the original file with that temporary file.
void SecondWindow::on_pushButton_8_clicked() { QFile file("C:/Users/umutc/Desktop/Test/text/test.txt"); QFile tmpFile("C:/Users/umutc/Desktop/Test/text/test_tmp.txt"); if (!file.open(QIODevice::Read | QIODevice::Text) || !tmpFile.open(QIODevice::Write | QIODevice::Text)) return; QTextStream tmp(&tmpFile); tmp << ui->newPassBox->text(); QTextStream in(&tmpFile); // Skip first line in.readLine(); while (!in.atEnd()) { tmp << in.readLine(); } // Now replace the original file with tmp file.close(); tmpFile.close(); QFile::remove("C:/Users/umutc/Desktop/Test/text/test.txt"); QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt"); }
-
@TheCeylann You will need to write new content into a temporary file, then replace the original file with that temporary file.
void SecondWindow::on_pushButton_8_clicked() { QFile file("C:/Users/umutc/Desktop/Test/text/test.txt"); QFile tmpFile("C:/Users/umutc/Desktop/Test/text/test_tmp.txt"); if (!file.open(QIODevice::Read | QIODevice::Text) || !tmpFile.open(QIODevice::Write | QIODevice::Text)) return; QTextStream tmp(&tmpFile); tmp << ui->newPassBox->text(); QTextStream in(&tmpFile); // Skip first line in.readLine(); while (!in.atEnd()) { tmp << in.readLine(); } // Now replace the original file with tmp file.close(); tmpFile.close(); QFile::remove("C:/Users/umutc/Desktop/Test/text/test.txt"); QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt"); }
@jsulm said in How can I delete first line of text file before write:
QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt");
hi i have used your code for replace the first line for text file. but it delete the whole content of file and replace the text of temp file.
why its not working ?
QString str1 = QDir::homePath() + "/Runtime/"+ "ReadTotalPowerOnTime1.txt"; QFile file(str1); if (!file.open(QIODevice::ReadWrite)) { qDebug() << "File Creation Failed"; qDebug()<< "Cannot open file for writing: " << qPrintable(file.errorString()); } QFile tmpFile("test_tmp.txt"); if(((!tmpFile.open(QIODevice::WriteOnly | QIODevice::Text)))) return; QTextStream tmp(&tmpFile); tmp <<"hi"; QTextStream in1(&tmpFile); // Skip first line in1.readLine(); while (!in1.atEnd()) { tmp << in1.readLine(); } // Now replace the original file with tmp file.close(); tmpFile.close(); QFile::remove(str1); QFile::rename("test_tmp.txt", str1);
-
@jsulm said in How can I delete first line of text file before write:
QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt");
hi i have used your code for replace the first line for text file. but it delete the whole content of file and replace the text of temp file.
why its not working ?
QString str1 = QDir::homePath() + "/Runtime/"+ "ReadTotalPowerOnTime1.txt"; QFile file(str1); if (!file.open(QIODevice::ReadWrite)) { qDebug() << "File Creation Failed"; qDebug()<< "Cannot open file for writing: " << qPrintable(file.errorString()); } QFile tmpFile("test_tmp.txt"); if(((!tmpFile.open(QIODevice::WriteOnly | QIODevice::Text)))) return; QTextStream tmp(&tmpFile); tmp <<"hi"; QTextStream in1(&tmpFile); // Skip first line in1.readLine(); while (!in1.atEnd()) { tmp << in1.readLine(); } // Now replace the original file with tmp file.close(); tmpFile.close(); QFile::remove(str1); QFile::rename("test_tmp.txt", str1);
@Qt-embedded-developer
I see you are opening the fileQIODevice::WriteOnly
.- You then attempt to read from this file. How is that going to work?
- Even if it did work, what is the point of reading from a file which you just opened for write and truncated to 0 length, you know what is going to be there anyway?
Please make your code sensible before asking for help, thanks. And take the time to read and understand what @jsulm has shown you, your code is not working because it is not the same as his and does not make sense.
UPDATE Actually, @jsulm's code is wrong. You should be able to spot where and correct, and/or he may see this and correct his post.
-
@Qt-embedded-developer
I see you are opening the fileQIODevice::WriteOnly
.- You then attempt to read from this file. How is that going to work?
- Even if it did work, what is the point of reading from a file which you just opened for write and truncated to 0 length, you know what is going to be there anyway?
Please make your code sensible before asking for help, thanks. And take the time to read and understand what @jsulm has shown you, your code is not working because it is not the same as his and does not make sense.
UPDATE Actually, @jsulm's code is wrong. You should be able to spot where and correct, and/or he may see this and correct his post.
-
-
Your
QIODevice::WriteOnly
is correct, never mind that @jsulm wroteQIODevice::Write
. This also applies forQIODevice::Read
->QIODevice::ReadOnly
. -
You open
file.open(QIODevice::ReadWrite)
for read-write. Why? You also removed theQIODevice::Text
. Why? -
The issue in @jsulm's code, replicated in yours, is that you & he have:
QTextStream tmp(&tmpFile); QTextStream in(&tmpFile);
If you take the time to think about it you will realize it cannot be right and will correct it.
-
-
-
Your
QIODevice::WriteOnly
is correct, never mind that @jsulm wroteQIODevice::Write
. This also applies forQIODevice::Read
->QIODevice::ReadOnly
. -
You open
file.open(QIODevice::ReadWrite)
for read-write. Why? You also removed theQIODevice::Text
. Why? -
The issue in @jsulm's code, replicated in yours, is that you & he have:
QTextStream tmp(&tmpFile); QTextStream in(&tmpFile);
If you take the time to think about it you will realize it cannot be right and will correct it.
@JonB said in How can I delete first line of text file before write:
QTextStream tmp(&tmpFile);
QTextStream in(&tmpFile);Yeah, that one I did not see.
But @Qt-embedded-developer should be able to fix that... -
-
@JonB said in How can I delete first line of text file before write:
QTextStream tmp(&tmpFile);
QTextStream in(&tmpFile);Yeah, that one I did not see.
But @Qt-embedded-developer should be able to fix that...@jsulm yes i have done it.