Reading file
-
Hi
I am trying to read a file using QFile.
After file open how to read the file .I have one C program in which it reads the file for particular size.
@read_bytes = read( fd, event_buf, sizeof(struct input_event)*EVENT_BUF_NUM); @
How do I perform this in Qt
Thanks
-
Check the "official documentation of QFile":http://qt-project.org/doc/qt-4.8/qfile.html. There is an "example how to read file":http://qt-project.org/doc/qt-4.8/qfile.html#reading-files-directly.
-
With Qt you could use "QTextStream":http://qt-project.org/doc/qt-4.8/qtextstream.html and "QDataStream":http://qt-project.org/doc/qt-4.8/qdatastream.html to access files very easily.
Some example code to show how to use "QFile":http://qt-project.org/doc/qt-4.8/qfile.html and QTextStream:
@
void readFileFunction()
{
QFile file("test.txt");if(!file.open(QIODevice::ReadOnly)) { qDebug() << "error opening file: " << file.error(); return; } QTextStream instream(&file); QString line = instream.readLine(); qDebug() << "first line: " << line; file.close(); return
}
@QDataStream works the same way but is used for accessing non-text data.