2

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.

4

2 回答 2

4

你应该使用一个QStackQString。由于它是一个 LIFO(后进先出)容器,我认为这就是您要搜索的内容。

按下每个字符串,然后弹出所有字符串:

QStack<QString> stack;
stack.push("first string");
stack.push("second string");
stack.push("third string");

while (!stack.isEmpty())
{
   QString string = stack.pop();
}

编辑:(关于要写入的 2 个文件的评论中的新信息)

使用 aQvector存储所有 QString。然后在for循环中访问第一个和最后一个元素,将它们写入每个文件中。这可以这样做:

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
}

希望这能回答你的问题。

于 2012-02-28T18:03:28.457 回答
2

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.

于 2012-02-28T17:50:07.443 回答