QFile(3) - 文件读写

    技术2024-07-20  65

    QT文件打开及读写,初步演示     1    bool QFile::open ( OpenMode mode ) [virtual]     2    bool QFile::open ( FILE * fh, OpenMode mode )     3    bool QFile::open ( int fd, OpenMode mode )     4    qint64 QFile::readData ( char * data, qint64 len ) [virtual protected]     5    qint64 QFile::readLineData ( char * data, qint64 maxlen ) [virtual protected]     6    qint64 QFile::writeData ( const char * data, qint64 len ) [virtual protected]     7    void QFile::close () [virtual]     8    bool QFile::flush ()     9    bool QFile::atEnd () const [virtual]     10    qint64 QFile::pos ()     11    bool QFile::resize ( qint64 sz )     QT博大精深。。。。样例很很简单。 例1:     文件         main.cpp     说明         简要调用几个API,能读能写,文本类型(Text)     代码             //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(); }

    最新回复(0)