gpt4 book ai didi

c++ - 默认写文件到桌面

转载 作者:行者123 更新时间:2023-11-30 02:46:44 25 4
gpt4 key购买 nike

C++ 中是否有默认代码将文件 (.txt) 写入桌面,可以在不知道前导/desktop 的情况下用于任何计算机?

最佳答案

最便携的方式是使用Qt,即QStandardPaths .

标准库没有对它的任何直接支持,因此您要么需要重新发明轮子,要么找到一个已经存在的可靠解决方案。 Qt就是这么个东西。

QStandardPaths::DesktopLocation 0 Returns the user's desktop directory.

在这种情况下,您可以使用 QFile 以及 ofstream 将文件写入该文件夹。为此,您只需要依赖于 QtCore

代码看起来像这样:

#include <QFile>
#include <QStandardPaths>
#include <QDebug>
#include <QTextStream>

...

QFile file(QStandardPaths::locate(QStandardPaths::DesktopLocation, ""));
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
qDebug() << "Failed to open";

QTextStream out(&file);

// Done, yay!

这将适用于 QtCore 支持的发行版和操作系统,包括但不限于:

  • window

  • Linux

  • 苹果电脑

  • QNX

等等。

关于c++ - 默认写文件到桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23321434/

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