欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++创建和删除文件夹的方法

程序员文章站 2022-06-14 15:44:05
...

一、说明:下面的测试代码在linux下运行通过

二、测试代码:
1、boost库和linux下的库函数

#include <iostream>
#include "boost/filesystem.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

using namespace std;
int main(int argc, char *argv[])
{
    const string exeFile = argv[0];
    cout << "exeFile:" << exeFile << endl;
    boost::filesystem::path tempExeFile(exeFile);
    const string exePath = tempExeFile.parent_path().string();
    cout << "exePath:" << exePath << endl;
    string tempDir = (tempExeFile.parent_path()/"tempFile").string();
    if(!boost::filesystem::exists(tempDir))
    {
        bool flag = boost::filesystem::create_directory(tempDir);
        if(flag)
        {
            cout << "tempDir:" << tempDir << endl;
            cout << "boost create success..." << endl;
        }
        else
        {
            cout << "boost create failure..." << endl;
        }
    }
    if(boost::filesystem::exists(tempDir))
    {
        bool flag = boost::filesystem::remove(tempDir);
        if(flag)
        {
            cout << "boost remove success..." << endl;
        }
        else
        {
            cout << "boost remove failure..." << endl;
        }
    }

    // linux function
    int index = exeFile.find_last_of('/');
    const string exePath2 = exeFile.substr(0,index);
    string tempDir2 = exePath2+"/tempFile2";
    if(access(tempDir2.c_str(),0) == -1)
    {
        int isSuccess = mkdir(tempDir2.c_str(),S_IRWXU);
        if(isSuccess == 0)
        {
            cout << "tempDir2: " << tempDir2 << endl;
            cout << "linux funciton create success!" << endl;
        }
        else
        {
            cout << "linux function create failure!" << endl;
        }
    }
    if(access(tempDir2.c_str(),0) == 0)
    {
        int isSuccess = rmdir(tempDir2.c_str());
        if(isSuccess == 0)
        {
            cout << "linux function remove success!" << endl;
        }
        else
        {
            cout << "linux function remove failure!" << endl;
        }
    }
    return 0;
}

二、Qt方法

#include <QCoreApplication>
#include <QDir>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString appPath = QCoreApplication::applicationDirPath();
    qDebug() << "appPath:" << appPath;
    QString tempDir = appPath+"/tempQdir";
    QDir pathDir(tempDir);
    if(!pathDir.exists())
    {
        bool isSuccess = pathDir.mkdir(tempDir);
        if(isSuccess)
        {
            qDebug() << "pathDir:" << pathDir << endl;
            qDebug() << "create pathDir success..." << endl;
        }
        else
        {
            qDebug() << "create pathDir failure" << endl;
        }
    }
    if(pathDir.exists())
    {
        bool isSuccess = pathDir.rmdir(tempDir);
        if(isSuccess)
        {
            qDebug() << "remove pathDir success..." << endl;
        }
        else
        {
            qDebug() << "remove pathDir failure..." << endl;
        }
    }
    return a.exec();
}