如何在 Qt 中创建具有完整路径的新文件?
我是 Qt 初学者,刚刚遇到了这个问题。我正在寻找一个文件 SomePath/NewDirectoryA/NewFile.kml
(NewFile.kml
将是 NewDirectoryA
中的唯一文件,该目录仅以维护项目中的语义)。
如果 SomePath/NewDirectoryA/NewFile.kml
存在,那么我将在我的代码中使用它,如果它不存在,那么我必须创建它。如果此文件不存在,则 SomePath
中也不存在此目录。因此,如果我必须创建一个文件,我可以使用 QFile 并以 ReadWrite 或 WriteOnly 模式打开它。
但问题是我必须使用目录本身创建文件。
我尝试使用文件名 SomePath/NewDirectoryA/NewFile.kml
的 QFile
但没有成功。
请建议我一种可以在给定位置 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Qt 关于文件创建的警告
目录是用以下命令创建的
和
Qt's caveat for file creation
Directories are created with
and
据我所知,无法直接使用 QFile 创建文件和目录。您必须首先创建目录(
QDir::mkpath
将创建完整路径),然后创建文件(QFile ::打开
)。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
).