zoukankan      html  css  js  c++  java
  • C++ QT note

    1. YT_11_QDir

    #include <QCoreApplication>
    #include<QDir>
    #include<QFileInfo>
    #include<QString>
    #include<QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QDir mDir;
        //foreach (QFileInfo mItem, mDir.drives() )
        //    qDebug() << mItem.  absoluteFilePath();
        QString mPath = "C:/text/ggg";
        if(!mDir.exists(mPath))
        {
            mDir.mkpath(mPath);
        }
        else
        {
            qDebug() << "Already exists.";
        }
        return a.exec();
    }

     创建目录 C:/text/ggg,如果目录不存在就创建它。

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QDir mDir("C:/Program Files");
        foreach(QFileInfo mItem, mDir.entryInfoList())
            qDebug() << mItem.absoluteFilePath();
        return a.exec();
    }

    获取指定路径下的全部文件夹路径作为列表,并输出该列表。

    2. YT_12_QFile // 使用QFile 读取 和 保存 txt文件以及内容。

    #include <QCoreApplication>
    #include <QFile>
    #include <QString>
    #include <QDebug>
    #include <QTextStream>
    
    void write(QString fullname)
    {
        QFile mFile(fullname);
        if(!mFile.open(QFile::WriteOnly | QFile::Text))
        {
            qDebug() << "Couldn't open this file for writing";
            return;
        }
        QTextStream out(&mFile);
        out << "Your Name: Guo Chao";
        mFile.flush();
        mFile.close();
    }
    
    void read(QString fullname)
    {
        QFile mFile(fullname);
        if(!mFile.open(QFile::ReadOnly | QFile::Text))
        {
            qDebug() << "Couldn't open the file for reading";
            return;
        }
        QTextStream in(&mFile);
        QString mText = in.readAll();
        qDebug() << mText;
    
        mFile.close();
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QString mFullname = "C:/test.txt";
    
        write(mFullname);
        read(mFullname);
    
        return a.exec();
    QFile_Writing&Reading
    
    
  • 相关阅读:
    [离散数学II]2017.5.9
    mysql内连接、左连接、右连接
    Android平台介绍
    软技能(面试)1
    流程控制练习题
    函数:算法
    linux系统文件
    App测试需注意
    python-循环
    python-正则表达式
  • 原文地址:https://www.cnblogs.com/TadGuo/p/8999092.html
Copyright © 2011-2022 走看看