Qt控制台例子

功能:实现通过命令行方式保存文件

#include <QCoreApplication>
#include <iostream>
#include <QString>
#include <QTextStream>
#include <QFile>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if(argc != 3)
    {
        cout << "please input three parameters!" << endl;
        cout << "For example:" << endl;
        cout << "	" << "./exportfile filename.txt address" << endl;
        return -1;
    }
    QString strFirst = argv[1];
    QString strSecond = argv[2];
    if(strFirst.endsWith(".txt"))
    {
        cout <<"export txt file!" << endl;
        QString fileName = strSecond + strFirst;
        QFile file(fileName);
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            return -1;
        }
        QTextStream stream(&file);
        stream << "hello" << "one" << "one" << endl;
        stream.flush();
        file.close();
    }
    return a.exec();
}
怕什么真理无穷,进一寸有一寸的欢喜。---胡适
原文地址:https://www.cnblogs.com/hujianglang/p/6635854.html