PDA

View Full Version : Readind textfiles



LordQt
26th August 2008, 11:48
Hello friends. I actually try to read textfiles.


QFile infile("C:/testfile.txt");

if (!infile.open(QIODevice::ReadOnly | QIODevice::Text))
{
bodyEdit->append("Can´t read file!!");
return;
}
while (!infile.atEnd())
{
bodyEdit->append(infile.readLine());
}


when I have a little file ( <3000-5000 lines) its ok, but when I have a textfile ( >100.000 lines) I have a problem with the performance.

Do I make a mistake or what is it??

Valheru
26th August 2008, 12:50
Personally I'd read the entire file into memory and do any parsing from there. You can do this using QTextStream::readBytes(). This obviously means creating a QTextStream and using it to read the QFile, which can be done as follows:



QFile("myfile.txt");
QString contents;

if( file.exists() ){
if( file.open( QIODevice::ReadOnly ) ){
QTextStream stream( &file );
contents = stream.readAll();
}
}

file.close();


This will use a lot of memory though according to the docs, but if you have GB's anyway then who cares?

LordQt
26th August 2008, 16:52
Ok thx a lot,

I will try it

Lesiok
26th August 2008, 17:32
Are You sure that problem is with reading file ? What is bodyEdit ? If You have text file with 100000 lines You must call 100000 times append method. Maybe this is a problem.

aamer4yu
26th August 2008, 17:48
what file you have for 100000 lines :O ??
if we assume each line consists of 50 chars, 100000*50 = 5000000 bytes ! ~~ 4GB file !
even if u read it in memory once, u might get problems,,,
better way wud be to implement some mechanism where you seek into the file and display required bytes,,, am not sure how :(

wysota
26th August 2008, 20:30
when I have a little file ( <3000-5000 lines) its ok, but when I have a textfile ( >100.000 lines) I have a problem with the performance.
QTextEdit::append is very slow. Use QTextCursor instead.


This will use a lot of memory though according to the docs, but if you have GB's anyway then who cares?

I do. This solution is far from being scalable as you have to allocate at least two times the length of your file. With big files this is unacceptable.