PDA

View Full Version : reading files



baray98
19th September 2007, 23:15
I have my files as input in my app (its a binary file). What i want to do is read the whole file and put it in a QVector <uchar> mydata. is there an easy way to do it in Qt?

please help me,

baray98

marcel
19th September 2007, 23:21
You can do it with a QFile and a QDataStream:


QFile f("c:\file.bin");
if(!f.open(QIODevice::ReadOnly))
return;

QDataStream stream(&f);
char *s = new char[(int)f.size()];
stream.readRawData(s, (int)f.size());


Now all the data is in s. Keep in mind that for big files you may get out of memory errors(new might fail) so the solution is to read the file in smaller buffers and process them sequentially.

QVector<uchar> is not really optimal in this case so you should use a plain char array.

wysota
20th September 2007, 12:31
Or a QByteArray in a bit simpler way:

QFile f("..");
f.open(QFile::ReadOnly);
QByteArray data = f.readAll();