使用QFile创建包含目录的文件

时间:2012-07-30 18:02:10

标签: qt

我想知道是否有可能一次性创建包含其目录的文件。例如,我想创建文件脚本/ myFile.txt。

我写了这样的代码:

QFile _file( path );
QDir _dir;

// check if "scripts" folder exists
int _dirExists = _dir.exists( "scripts" );
// if not, create it
if( !_dirExists )
    _dir.mkdir( "scripts" );

// open file in write mode (and text mode) 
int _fileOpened = _file.open( QIODevice::WriteOnly | QIODevice::Text );
if( !_fileOpened ) {
// ...

但我不得不使用QDir类,我不喜欢它的样子。我无法理解为什么QFile不像大多数这种框架那样创建必要的目录。或许我错过了什么?

2 个答案:

答案 0 :(得分:8)

我知道它多年的事后,但QDir :: mkpath刚刚为我工作过。

http://qt-project.org/doc/qt-4.8/qdir.html#mkpath

答案 1 :(得分:5)

不,我相信你不能一次创建文件并且它包含目录。

相关问题