C++ QT note

1. YT_11_QDir

#include 
#include
#include
#include
#include

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 
#include 
#include 
#include 
#include 

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

 

 

你可能感兴趣的:(C++ QT note)