QFile文件操作-QT
#include <QCoreApplication> #include<QFile> #include<QString> #include<QDebug> #include<QTextStream> void Write(QString Filename) { QFile mFile(Filename); if(!mFile.open(QFile::WriteOnly | QFile::Text)) { qDebug()<<"Could not open file for writing."; return; } QTextStream out(&mFile); out<<"Hello Boatlet hello cat"; mFile.flush(); mFile.close(); } void Read(QString Filename) { QFile mFile(Filename); if(!mFile.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"Could not open file for reading."; return; } QTextStream in(&mFile); QString mText = in.readAll(); qDebug() <<mText; mFile.close(); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //目录存在时,可以创建文件成功,按照"w"操作 QString mFilename = "E:/a/a01/01.txt"; Write(mFilename); Read(mFilename);
//Read("E:/test/QT/lesson001/Files/Files.pro");
return a.exec(); }