Qt 创建文件 用QFile和QDir类

项目需求是 根据日期创建多级子文件夹, 根目录保存 log.txt

控制台程序实现

如下执行结果

createFile 为程序目录

文档结构:

 

源码:


#include <QtCore/QCoreApplication>
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QDateTime>
#include <QObject>

void createFile(QString filePath,QString fileName)
{
    QDir tempDir;
    //临时保存程序当前路径
    QString currentDir = tempDir.currentPath();
    //如果filePath路径不存在,创建它
    if(!tempDir.exists(filePath))
    {
        qDebug()<<QObject::tr("不存在该路径")<<endl;
        tempDir.mkpath(filePath);
    }
    QFile *tempFile = new QFile;
    //将程序的执行路径设置到filePath下
    tempDir.setCurrent(filePath);
    qDebug()<<tempDir.currentPath();
    //检查filePath路径下是否存在文件fileName,如果停止操作。
    if(tempFile->exists(fileName))
    {
        qDebug()<<QObject::tr("文件存在");
        return ;
    }
    //此时,路径下没有fileName文件,使用下面代码在当前路径下创建文件
    tempFile->setFileName(fileName);
    if(!tempFile->open(QIODevice::WriteOnly|QIODevice::Text))
    {
        qDebug()<<QObject::tr("打开失败");
    }
    tempFile->close();
    //将程序当前路径设置为原来的路径
    tempDir.setCurrent(currentDir);
    qDebug()<<tempDir.currentPath();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    // 获取当前日期
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy/MM/dd");

    QString fileDir = "./Log/" + current_date;

    createFile(fileDir, "log.txt");

    return a.exec();
}


 

  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Qt中,可以使用QFileQDir来实现文件和目录的改变。QFile提供了一组用于操作文件的函数,可以创建、打开、读取、写入和关闭文件等操作。QDir提供了一组用于操作目录的函数,可以创建、删除、重命名、遍历目录等操作。以下是一个示例代码: ```cpp #include <QCoreApplication> #include <QDebug> #include <QFile> #include <QDir> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString filePath = "C:/Users/abc/Desktop/test.txt"; QString dirPath = "C:/Users/abc/Desktop/testdir"; // 创建文件 QFile file(filePath); if(file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); out << "Hello, world!"; file.close(); qDebug() << "Create file successfully."; } else { qDebug() << "Create file failed."; } // 创建目录 QDir dir; if(dir.mkdir(dirPath)) { qDebug() << "Create directory successfully."; } else { qDebug() << "Create directory failed."; } // 重命名文件 QString newFilePath = "C:/Users/abc/Desktop/newtest.txt"; if(file.rename(newFilePath)) { qDebug() << "Rename file successfully."; } else { qDebug() << "Rename file failed."; } // 删除目录 if(dir.rmdir(dirPath)) { qDebug() << "Remove directory successfully."; } else { qDebug() << "Remove directory failed."; } return a.exec(); } ``` 在上面的示例代码中,我们首先定义了一个文件路径和一个目录路径。然后,我们使用QFile创建了一个文件,并使用QTextStream向文件中写入了一些文本。接着,我们使用QDir创建了一个目录。然后,我们使用QFile的rename函数将文件重命名为新的文件名。最后,我们使用QDir的rmdir函数删除了目录。 注意:在进行文件和目录的改变时,需要先判断操作是否成功,可以通过QFileQDir提供的返回值来判断。另外,在重命名文件时,需要使用QFile的rename函数,而不能直接使用QFile的renameTo函数。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值