学会思考
刻意练习

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

#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 << "\t" << "./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();
}

 

posted on 2017-03-28 17:18  Worty  阅读(428)  评论(0编辑  收藏  举报