Copy and paste contents of one file to another and modify
-
Hi All,
I'm a bit stuck in a problem. I need to do the following file operation
- Need to make a new file and copy content from an old file + modify it by adding a few more extra bits.
- Delete the old file.
Here I could open the file in
readwrite
mode and read line by line.void MainWindow::on_pushButton_readlog_clicked() { QString filename = "oldfile.txt"; QFile originalFile(filename); if(!originalFile.open(QIODevice::ReadWrite )) { qDebug () << "Error opening Log file: "<<originalFile.errorString(); return; } else { QTextStream instream(& originalFile); QString line = instream.readLine(); while(!instream.atEnd()) { QString line =instream.readLine(); // I can read line by line qDebug()<<line; } originalFile.close(); } }
How do I copy this contents into a new file and add few more lines of texts to it?
Say line 1: "Name: new name"
line 2: "Device ID: new ID" -
@russjohn834 said in Copy and paste contents of one file to another and modify:
How do I copy this contents into a new file
https://doc.qt.io/qt-5/qfile.html#copy-1
"and add few more lines of texts to it?" - by opening the file for writing and appending the lines. What exactly is the problem?
-
@russjohn834
You cannot do this by opening the existing file for read-write. As you correctly say you must copy from old to new:- Open existing file for read.
- Open new file for write.
- Read line by line from old file, writing to new file. Make any amendments (e.g. changing a line or inserting a new line) as you go.
- Close the new file.
- Close the old file.
- Delete the old file.
- Now optionally rename the new file to something like the old file name if that is desired.
-
@JonB said in Copy and paste contents of one file to another and modify:
- Read line by line from old file, writing to new file. Make any amendments (e.g. changing a line or inserting a new line) as you go.
That's where I'm stuck!
Contents from the existing file looks like this:
=== USAGE_LOG === Power cycles: 1 Unpause events: 1 Power-on time: 29860s Unpaused time: 8s 0 1 8074 8074 8074 1 0 4294967295 0 0 2 0 4294967295 0 0 3 0 4294967295 0 0 4 0 4294967295 0 0 5 0 4294967295 0 0 6 0 4294967295 0 0 7 0 4294967295 0 0 8 0 4294967295 0 0 9 0 4294967295 0 0 10 0 4294967295 0 0 11 0 4294967295 0 0 12 0 4294967295 0 0 13 0 4294967295 0 0 14 0 4294967295 0 0 15 0 4294967295 0 0 16 0 4294967295 0 0 17 0 4294967295 0 0
Basically I need to insert two new lines (before "=== USAGE_LOG==="). Looks like I need to get read line by line and store into a string list. Not I'm not sure how to "iterate" over the string list and modify(or insert) lines. Can you show me a way to do this?
-
@russjohn834 Simply open a temp file, write the new lines, then read line by line the old file and write these lines into temp file. Then replace the old file with the temp file (https://doc.qt.io/qt-5/qfile.html#remove-1, https://doc.qt.io/qt-5/qfile.html#rename-1).
To add an entry at the beginning of a list use https://doc.qt.io/qt-5/qlist.html#prepend, iterating over a list is as easy as
for (QString str : strList) { }
-
I did the following : I stuck at two places:
-
How to copy contents line by line from old file to new file
(with my implementation below, it's simply copying everything from old file as single line in new file) -
If I simply do
outstream<<line;
I'm not sure how to modify it's contents.
Do you think StringList is better in this case or is there a better way to do this?
QString filename = "oldfile.txt"; QString newfilename = "newfile.txt"; QFile originalFile(filename), newFile(newfilename); originalFile.open(QIODevice:: ReadOnly); newFile.open(QIODevice:: WriteOnly); QTextStream instream(& originalFile); QTextStream outstream(& newFile); while(!instream.atEnd()) { QString line = instream.readLine(); outstream<<line; // all the contents are copied as a single line here } originalFile.close(); newFile.close();
-
-
@russjohn834
Your code looks good.instream.readLine();
should return each line separately. Then all would be well. If you cannot correctly read lines you would have the same problem if you useQStringList
. Check the contents of your input file (preferably in a binary editor) for what the exact line terminator is.If you are on Windows (or possibly MacOS too, and it does no harm for Linux anyway) you should pass
QIODevice::Text
when opening the file for read. I think your failure to do so might lead to the behaviour you see (whole file as one line). That flag should also be on the file you are writing to.