QT操作文件写入文件

QT操作文件写入文件

#include "File_Operation.h"
#include "qfile.h"
#include <QDebug>
#include <QDateTime>

File_Operation::File_Operation()
{

}
File_Operation::~File_Operation()
{

}
QString File_Operation::OpenTxtFile(QString write_content)
{
    QFile file("qt_file5.txt");

   // if(!file.open(QIODevice::WriteOnly|QIODevice::Text))//只写方式打开,文件不存在则创建
         if(!file.open(QIODevice::Append|QIODevice::Text))
    {
        qDebug()<<file.errorString();
       file.write("hello QT\nby txwtech--first time");
        return "error";
    }
    else
    {
        QString addInfo = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz")+write_content+"\n";
        char *pAddinfo;
        QByteArray qt_byte=addInfo.toUtf8();
        pAddinfo=qt_byte.data();
        file.write(pAddinfo);
      //  file.Append("追加数据1");
        return "ok";
    }
}
#ifndef FILE_OPERATION_H
#define FILE_OPERATION_H
#include <QString>

class File_Operation
{
   public:
    File_Operation();
    ~File_Operation();
    QString OpenTxtFile(QString write_content);

};

#endif // FILE_OPERATION_H

 

posted @ 2022-07-25 21:14  txwtech  阅读(1805)  评论(0编辑  收藏  举报