qt - QT中反向写入文本文件

标签 qt reverse qfile

如何更好地实现以相反的顺序逐个字符串地写入 QFile。

使用 seek(0) 新字符串覆盖旧字符串。

最佳答案

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

压入每个字符串,然后弹出所有字符串:

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

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

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

使用 Qvector存储所有 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
}

希望这能回答您的问题。

关于qt - QT中反向写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487059/

相关文章:

c++使用动态分配运算符反转数组元素

c - 我想在用户输入值时获取数组的倒数

c++ - 将 QFiles 正确地存储在一个集合中

c++ - QFile.write(myStruct)-怎么样?

qt - 带有复选框的 QComboBox

c++ - 如何连接QTableView选择改变的信号

c# - 在 C# 中反转单链表

c++ - QFile 需要多个参数?

c++ - imread 不能在 qt 上工作

c++ - Opencv,对 `cv::imread(??cv::String const&???, int)' 的 undefined reference