gpt4 book ai didi

c++ - 为 Qt 写入的文本文件选择自定义行结尾

转载 作者:太空狗 更新时间:2023-10-29 20:36:24 26 4
gpt4 key购买 nike

在 Qt 中写入文本文件时(使用 QFile 和 QTextStream),任何 \nendl 都会自动转换为正确的平台特定行结尾(例如 \r\n 适用于 Windows)。

我想让用户选择使用哪个文件结尾。

有没有办法在不使用二进制文件模式的情况下设置以 Qt 结尾的所需行?

最佳答案

不,没有。文本模式的含义是“对平台的这些进行行尾更改”。如果您想做任何其他事情,请使用二进制模式,并通过重新实现例如QFile::writeDataQFile::readData

template <class Base> class TextFile : public Base {
QByteArray m_ending;
qint64 writeData(const char * data, qint64 maxSize) override {
Q_ASSERT(maxSize <= std::numeric_limits<int>::max());
QByteArray buf{data, maxSize};
buf.replace("\n", m_ending.constData());
auto len = Base::writeData(buf.constData(), buf.size());
if (len != buf.size()) {
// QFile/QSaveFile won't ever return a partial size, since the user
// such as `QDataStream` can't cope with it.
if (len != -1) qWarning() << "partial writeData() is not supported for files";
return -1;
}
return len;
}
...
}

TextFile<QFile> myFile;
...

关于c++ - 为 Qt 写入的文本文件选择自定义行结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38777334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com