在上节中,我们实现了再某一个位置创建文件夹和文件名,这节来实现文件的操作和向文件写内容。
首先看源代码:
#include <QCoreApplication>
#include <QtDebug>
#include <QDir>
#include <QString>
#include <QFile>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// QString mStr = "hello,www.eepw.com.cn";
QDir Mkpath;
QString myPath="/home/mjl/QT_exam/lesson2/data";
if( !Mkpath.exists(myPath))
{
Mkpath.mkpath(myPath);
qDebug() << Mkpath.exists(myPath);
}
qDebug()<<"the path is exist!";
QFile file("/home/mjl/QT_exam/lesson2/data/data.txt");
if(file.exists())
{
qDebug()<<"creat success!";
}
file.open(QIODevice::ReadWrite | QIODevice::Text);
QTextStream out(&file);
out << "hello,eepw";
file.flush();
file.close();
return a.exec();
}
我要赚赏金
