Reverse text file writing in QT
Pergunta
How is it better to implement writing to QFile in reverse order, string by string. With use of seek(0) new string is written over old one.
Solução
You should use a QStack
of QString. Since it's a LIFO (Last In First Out) container I think it's what you are searching for.
Push each string as they come and then pop all strings:
QStack<QString> stack;
stack.push("first string");
stack.push("second string");
stack.push("third string");
while (!stack.isEmpty())
{
QString string = stack.pop();
}
Edit: (new information in comments concerning the 2 files to write)
Use a Qvector
to store all QString. Then in a for loop access first and last element to write them in each file. This can be done like this:
QVector<QString> vector;
vector.append("first string");
vector.append("second string");
vector.append("third string");
int size = vector.size();
for (int i=0; i<size; i++)
{
QString string1 = vector[0]; // write it in one file
Qstring string2 = vector[size-1-i]; // write it in another file
}
Hope that answers your question.
Outras dicas
Read all the file. Save it to a vector of QStrings in the memory. Go trough the vector reversing all the strings and save this vector to the file again.