gpt4 book ai didi

c++ - Qt 写入 xml:QIODevice::write (QFile, "D:/logs.xml"): device not open

转载 作者:可可西里 更新时间:2023-11-01 11:23:33 29 4
gpt4 key购买 nike

我正在尝试将数据保存到 Qt 中的 XML 文件中。到处都被告知我需要写这样的检查:

if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Failed to open";
}
else
{
//write to file;
}

我总是得到“无法打开”的结果。我就是找不到任何相关信息。如果我不使用这个检查,我会得到这样的错误:QIODevice::write (QFile, "D:/logs.xml"): device not open。我只是不知道该怎么办。这是我在教程中尝试使用的完整代码:

QDomDocument document;
// Making the root element
QDomElement root = document.createElement("Dorms");

// Adding the root element to the docuemnt
document.appendChild(root);
QFile file;
file.setFileName("D:/logs.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Failed to open";
}
else
{
QTextStream stream(&file);
stream << document.toString();
file.close();
qDebug() << "Done";
}

有谁知道我做错了什么?

最佳答案

原因

您在 Windows 中使用了错误的斜杠。

解决方案

改变

file.setFileName("D:/logs.xml");

file.setFileName("D:\logs.xml");

关于c++ - Qt 写入 xml:QIODevice::write (QFile, "D:/logs.xml"): device not open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52609825/

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