利用qt的QFile读文本文件

今天主要学习用qt里面的QFile来读取文本内容。第一步我们首先新建一个项目选择qt控制台应用 新建一个项目。

接着就是在main.cpp里面添加代码如下:

#include <QCoreApplicatoin>

#include <QFile>

#inlcude <QtDebug>

int main(int argc, argv)

{

    QCoreApplication a(argc, argv);

    QFile file("hello.txt");     //这句打开一个名为hello.txt的文本文件,该文件应保存在同项目路径下面的build-                                             //  extFile-Destop_QT_5_4_..........._Debug目录里面

    if (file.open(QIODevice::ReadOnlly))  //这里的ReadOnly是只读的意思

            char buffer[2048];

            qin64 lineLen = file.readLine(buffer, sizeof(buffer));

            if (lineLen != -1)

            {

                 qDebug() << buffer;

            }

              reture a.exec();

   }    

}

运行结果如下,里面的Hello world是我在hello.txt文件里面写的一句。




猜你喜欢

转载自blog.csdn.net/weixin_39770778/article/details/80619087
今日推荐