QTextStream 读写文件

/***************************************
* QIODevice::ReadWrite 可读写
* QIODevice::Text 换行符生效
* QIODevice::Append 追加写入
* QFile::Truncate 表示将原文件清空
****************************************/
void MainWindow::test()
{

ifdef QTextStream_read

QFile file("e:\\file.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
    qDebug()<<"open fail!";
    return;
}
QTextStream in(&file);
//    QString line1= in.read(1); //读取第一个字符
//    qDebug()<

else

QString LogFile;
QString runPath = QCoreApplication::applicationDirPath();       //
LogFile.clear();
LogFile.append(tr("%1/LogFile_%2.txt").arg(runPath).arg(QDate::currentDate().toString("yyyyMMdd")));


QFile file(LogFile);//"e:\\file.txt"
if (!file.open(QIODevice::WriteOnly | QIODevice::Text |QIODevice::Append)){
    qDebug()<<"open fail!";
    return;
}
QTextStream out(&file);
out<<"test"<

endif

}

你可能感兴趣的:(QT)