如何在 Qt 中创建具有完整路径的新文件?

发布于 2024-09-04 22:03:31 字数 551 浏览 10 评论 0原文

我是 Qt 初学者,刚刚遇到了这个问题。我正在寻找一个文件 SomePath/NewDirectoryA/NewFile.kmlNewFile.kml 将是 NewDirectoryA 中的唯一文件,该目录仅以维护项目中的语义)。

如果 SomePath/NewDirectoryA/NewFile.kml 存在,那么我将在我的代码中使用它,如果它不存在,那么我必须创建它。如果此文件不存在,则 SomePath 中也不存在此目录。因此,如果我必须创建一个文件,我可以使用 QFile 并以 ReadWrite 或 WriteOnly 模式打开它。

但问题是我必须使用目录本身创建文件。
我尝试使用文件名 SomePath/NewDirectoryA/NewFile.kmlQFile 但没有成功。

请建议我一种可以在给定位置 (SomePath) 的新目录 (NewDirectorA) 中创建新文件 (NewFile.kml) 的方法。

I am a Qt beginner and just got stuck with the problem. I am looking for a file SomePath/NewDirectoryA/NewFile.kml (NewFile.kml will be the only file in NewDirectoryA, having this directory just to maintain semantics in the project).

If SomePath/NewDirectoryA/NewFile.kml exists then I will use it in my code and if it doesn't exist then I have to create it. If this File doesn't exist then this directory also doesn't exist in SomePath. So If only I have to create a file I can use QFile and open it in ReadWrite or WriteOnly mode.

But the problem is I have to create the file with the directory itself.
I tried with QFile with file name SomePath/NewDirectoryA/NewFile.kml but it didn't worked.

Please suggest me a way in which I can create a new file (NewFile.kml) in a new directory (NewDirectorA) at a given location (SomePath).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-09-11 22:03:31

bool QFile::open ( OpenMode模式)[虚拟]

[...]

注意:在 WriteOnly 或 ReadWrite 模式下,
如果相关文件还没有
存在,该函数将尝试
在打开之前创建一个新文件。

Qt 关于文件创建的警告

平台特定问题

在类 Unix 系统上文件权限的处理方式不同
视窗。在类 Unix 系统上的不可写目录中,文件
无法创建。在 Windows 上情况并非总是如此,对于
例如,“我的文档”目录通常不可写,但它
仍然可以在其中创建文件。

目录是用以下命令创建的

布尔值
QDir::mkdir
( const QString & dirName ) const

创建一个名为的子目录
目录名称。

bool QDir::mkpath
( const QString & dirPath ) const

创建目录路径dirPath。

该函数将创建所有父级
创建所需的目录
目录。

bool QFile::open ( OpenMode mode ) [virtual]

[...]

Note: In WriteOnly or ReadWrite mode,
if the relevant file does not already
exist, this function will try to
create a new file before opening it.

Qt's caveat for file creation

Platform Specific Issues

File permissions are handled differently on Unix-like systems and
Windows. In a non writable directory on Unix-like systems, files
cannot be created. This is not always the case on Windows, where, for
instance, the 'My Documents' directory usually is not writable, but it
is still possible to create files in it.

Directories are created with

bool
QDir::mkdir
( const QString & dirName ) const

Creates a sub-directory called
dirName.

and

bool QDir::mkpath
( const QString & dirPath ) const

Creates the directory path dirPath.

The function will create all parent
directories necessary to create the
directory.

毁我热情 2024-09-11 22:03:31

据我所知,无法直接使用 QFile 创建文件和目录。您必须首先创建目录(QDir::mkpath 将创建完整路径),然后创建文件(QFile ::打开)。

QString path("SomePath/NewDirectoryA/");
QDir dir; // Initialize to the desired dir if 'path' is relative
          // By default the program's working directory "." is used.

// We create the directory if needed
if (!dir.exists(path))
    dir.mkpath(path); // You can check the success if needed

QFile file(path + "NewFile.kml");
file.open(QIODevice::WriteOnly); // Or QIODevice::ReadWrite

AFAIK it is not possible to create the file and the directory directly with QFile. You have to first create the directory (QDir::mkpath will create the full path) and then the file (QFile::open).

QString path("SomePath/NewDirectoryA/");
QDir dir; // Initialize to the desired dir if 'path' is relative
          // By default the program's working directory "." is used.

// We create the directory if needed
if (!dir.exists(path))
    dir.mkpath(path); // You can check the success if needed

QFile file(path + "NewFile.kml");
file.open(QIODevice::WriteOnly); // Or QIODevice::ReadWrite
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文