Issues modifying QFile information
-
Hello, I'm trying to open a QFile that contains lines of data that represent intel hex and make some changes,
every line starts with a colon, and I am trying to replaced the colon with 0x
here is what I have, which seems to result in an empty file from what I can tell based on my output file:
QFile filea(file1); QFile fileb(file2); QFile result(loc); if(filea.open(QIODevice::ReadWrite) && fileb.open(QIODevice::ReadWrite) && result.open(QIODevice::ReadWrite)){ //process the filea QTextStream in1(&filea); while(!in1.atEnd()){ QString line = in1.readLine(); line.remove(0,1); line.prepend("0x"); qDebug() << line; in1<< line <<endl; } QByteArray ba; QTextStream ts(&ba, QIODevice::ReadWrite); // merge files into textstream ts << filea.readAll(); ts << fileb.readAll(); result.write(ba);
Am i missing something where i push my QTextStream in1 back into filea?
-
This should not create the empty file. It should append the content at the end of the file. Looks like you did not close the filea handle once taks is over. Just try filea.close() and see what happens.
-
Hello, I'm trying to open a QFile that contains lines of data that represent intel hex and make some changes,
every line starts with a colon, and I am trying to replaced the colon with 0x
here is what I have, which seems to result in an empty file from what I can tell based on my output file:
QFile filea(file1); QFile fileb(file2); QFile result(loc); if(filea.open(QIODevice::ReadWrite) && fileb.open(QIODevice::ReadWrite) && result.open(QIODevice::ReadWrite)){ //process the filea QTextStream in1(&filea); while(!in1.atEnd()){ QString line = in1.readLine(); line.remove(0,1); line.prepend("0x"); qDebug() << line; in1<< line <<endl; } QByteArray ba; QTextStream ts(&ba, QIODevice::ReadWrite); // merge files into textstream ts << filea.readAll(); ts << fileb.readAll(); result.write(ba);
Am i missing something where i push my QTextStream in1 back into filea?
Am i missing something where i push my QTextStream in1 back into filea?
QString line = in1.readLine(); in1<< line <<endl;
I don't understand. You are reading a line from
in1
, and you are later writing a line into the samein1
. You can't do that! (Well, you can, but it won't behave as I'm suspecting you might expect it to!).You are changing one character in each line to two characters. Therefore, if you think it through, you will realise you cannot update back to the same file as you are reading from it. You need to change to either of the following (each with pros & cons):
-
Open the file for read. Read the whole file into memory. Close the file. Re-open the file for write. Output all the lines, with your changes. Close the file.
-
Open the file for read. Open a different, temporary file (often in the same directory as the original) for write. Read lines from the first file, outputting the changed lines to the new file as you go. Close both files. Delete the original file, and rename the new file to the original file now.
-