- //main.cpp
- #include <QtCore/QCoreApplication>
- #include <QFile>
- #include <QDebug>
- int main(int argc, char *argv[])
- {
- QCoreApplication app(argc, argv);
- QFile *file = new QFile();
- //task1 写入文件
- file->setFileName("/other/test/gw");
- file->open(QIODevice::WriteOnly | QIODevice::Text);
- file->reset();
- file->write("just have a test!/nhello!");
- file->close();
- QFile *file2 = new QFile();
- file2->setFileName("/other/test/gw");
- file2->open(QIODevice::ReadOnly | QIODevice::Text);
- file2->reset();
- QString str;
- //task2 每次读取100个
- qDebug() << "task2" << endl;
- while (!file2->atEnd())
- {
- str = file2->read(100);
- qDebug() << str;
- }
- file2->reset();
- //task3 每次读取一行
- qDebug() << "task3" << endl;
- while (!file2->atEnd())
- {
- str = file2->readLine(0);
- qDebug() << file2->pos();
- qDebug() << str;
- }
- file2->close();
- file->deleteLater();
- QObject::connect(file,SIGNAL(destroyed()),file2,SLOT(deleteLater()));
- QObject::connect(file2,SIGNAL(destroyed()),&app,SLOT(quit()));
- return app.exec();
- }
QFile文件读写
最新推荐文章于 2024-05-17 05:48:38 发布