如何在Qt中创建具有完整路径的新文件? [英] How to create a new File with full path in Qt?

查看:267
本文介绍了如何在Qt中创建具有完整路径的新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt初学者,只是被问题困扰.我正在寻找文件SomePath/NewDirectoryA/NewFile.kml(NewFile.kml将是NewDirectoryA中的唯一文件,该目录仅用于维护项目中的语义).

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).

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

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.

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

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.

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

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).

推荐答案

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

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

[...]

注意:在WriteOnly或ReadWrite模式下, 如果相关文件还没有 存在,此功能将尝试 在打开文件之前先创建一个新文件.

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关于文件创建的警告

Qt's caveat for file creation

平台特定问题

Platform Specific Issues

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

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.

目录创建

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

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

创建一个名为的子目录 dirName.

Creates a sub-directory called dirName.

布尔 QDir :: mkpath (const QString& dirPath)const

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

创建目录路径dirPath.

Creates the directory path dirPath.

该函数将创建所有父对象 创建目录所需的目录 目录.

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

这篇关于如何在Qt中创建具有完整路径的新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆