gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:10:02 25 4
gpt4 key购买 nike

我想知道是否可以一次性创建文件及其目录。例如我想创建文件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 不像大多数此类框架那样自己创建必要的目录。或者也许我错过了什么?

最佳答案

我知道这是战后的几年,但 QDir::mkpath 刚刚为我工作。

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

关于qt - 使用 QFile 创建带有目录的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11727006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com