How to read data from a file in Qt
This code snippet demonstrates how to read a file using QFile.
Article Metadata
Tested with
Devices(s): Emulator
Compatibility
Platform(s): Qt
Article
Keywords: QFile
Created: james1980
(07 Jan 2009)
Last edited: hamishwillee
(11 Oct 2012)
Description
- Make a text file and give full path in the program.
- The following code snippet would read this file and display its content as a text label.
Source File
#include <QFile>
#include <QApplication>
#include <QLabel>
#include <QString>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFile file("c://in.txt");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QString line = in.readLine();
QLabel label(line);
label.show();
return app.exec();
}



15 Sep
2009
This article is one of the very basic example of QT. Here they used Qfile class for reading data form a file.in Qt Qfile useful for reading from and writing to files.
This class can be used by it self.But hear they used it with QTextStream for displaying Hello.You can check for a file's existence using exists(), and remove a file using remove().
The file is opened with open(), closed with close(), and flushed with flush(). Data is usually read and written using QDataStream or QTextStream so you can use any of this command for making different operation with use of Qt.This article is very useful for beginner.
28 Sep
2009
Application may need to save some text data on client. Flat text file is more suitable in storing large data. Creating a text file and saving text data, in it for later use, is good option. Reading text file is basic operation to use text file. QFile is key API to read data from file in Qt.
The article describes methods to read data from text file. Before opening a file in ReadOnly mode one must check existence of a file, Opening a file in WriteOnly or ReadWrite mode will create a file if the relevant file does not already exist, but ReadOnly mode will gives error if file does not already exist.
Drg0072 -
it just shows only first line of file givendrg0072 15:47, 5 April 2012 (EEST)