找回密码
 立即注册
收起左侧

如何将应用程序输出框中的内容存储到txt文件当中?

3
回复
9186
查看
[复制链接]
累计签到:14 天
连续签到:1 天
来源: 2016-3-7 16:42:07 显示全部楼层 |阅读模式
1Qter豆

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-7 20:51:45 显示全部楼层
#include <QtCore/QCoreApplication>
#include <QFileInfo>
#include <QTextCodec>
#include <QStringList>
#include <QDateTime>
#include <QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

    // 以只写方式打开,如果文件不存在,那么会创建该文件
    QFile file("myfile.txt");
    if (!file.open(QIODevice::WriteOnly  | QIODevice::Text))
        qDebug() << file.errorString();
    file.write("helloQt!\nyafeilinux");
    file.close();


    // 获取文件信息
    QFileInfo info(file);
    qDebug() << QObject::tr("绝对路径:") << info.absoluteFilePath() << endl
            << QObject::tr("文件名:") << info.fileName() << endl
               << QObject::tr("基本名称:") << info.baseName() << endl
                  << QObject::tr("后缀:") << info.suffix() << endl
                     << QObject::tr("创建时间:") << info.created() << endl
                        << QObject::tr("大小:") << info.size();

    // 以只读方式打开
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        qDebug() << file.errorString();
    qDebug() << QObject::tr("文件内容:") << endl << file.readAll();
    qDebug() << QObject::tr("当前位置:") << file.pos();
    file.seek(0);
    QByteArray array;
    array = file.read(5);
    qDebug() << QObject::tr("前5个字符:") << array
                << QObject::tr("当前位置:") << file.pos();
    file.seek(15);
    array = file.read(5);
    qDebug() << QObject::tr("第16-20个字符:") << array;
    file.close();

    return a.exec();
}
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-7 20:56:58 显示全部楼层
QFile data("output.text");
if ( data.open(QFile::Writeonly | QFile::Truncate))
{
QTextStream out(&data);
out << "Result:" << QSetFieldWridth(10) << left << 3.14 << 2.7;
}
回复

使用道具 举报

累计签到:14 天
连续签到:1 天
2016-3-8 09:39:18 显示全部楼层
这个我也实现了,我说的是将调试信息输出到相应文件当中。网上搜到代码了,可是老是有错误

[cpp] view plain copy print?
#include <QtDebug>  
#include <QFile>  
#include <QTextStream>  
  
void customMessageHandler(QtMsgType type, const char *msg)  
{  
        QString txt;  
        switch (type) {  
        //调试信息提示  
        case QtDebugMsg:  
                txt = QString("Debug: %1").arg(msg);  
                break;  
  
        //一般的warning提示  
        case QtWarningMsg:  
                txt = QString("Warning: %1").arg(msg);  
        break;  
        //严重错误提示  
        case QtCriticalMsg:  
                txt = QString("Critical: %1").arg(msg);  
        break;  
        //致命错误提示  
        case QtFatalMsg:  
                txt = QString("Fatal: %1").arg(msg);  
                abort();  
        }  
  
        QFile outFile("debuglog.txt");  
        outFile.open(QIODevice::WriteOnly | QIODevice::Append);  
        QTextStream ts(&outFile);  
        ts << txt << endl;  
}  
  
int main( int argc, char * argv[] )  
{  
        QApplication app( argc, argv );  
  
        //先注册自己的MsgHandler  
        qInstallMsgHandler(customMessageHandler);         
         
        //以后就可以像下面这样直接打日志到文件中,而且日志也会包含时间信息  
        qDebug("This is a debug message at thisisqt.com");  
        qWarning("This is a warning message  at thisisqt.com");  
        qCritical("This is a critical message  at thisisqt.com");  
        qFatal("This is a fatal message at thisisqt.com");  
  
        return app.exec();  
}  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了