使用QFile创建带有目录的文件

7
我想知道是否可以一次性创建带有目录的文件。例如,我想要创建文件scripts/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个回答

10

这将创建完整的路径,但您无法创建文件,只能创建目录。 - ymoreau

6

不,我认为你不能一次性创建文件及其所在的目录。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接