Results 1 to 6 of 6

Thread: Readind textfiles

  1. #1
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Readind textfiles

    Hello friends. I actually try to read textfiles.
    Qt Code:
    1. QFile infile("C:/testfile.txt");
    2.  
    3. if (!infile.open(QIODevice::ReadOnly | QIODevice::Text))
    4. {
    5. bodyEdit->append("Can´t read file!!");
    6. return;
    7. }
    8. while (!infile.atEnd())
    9. {
    10. bodyEdit->append(infile.readLine());
    11. }
    To copy to clipboard, switch view to plain text mode 

    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??

  2. #2
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Readind textfiles

    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:

    Qt Code:
    1. QFile("myfile.txt");
    2. QString contents;
    3.  
    4. if( file.exists() ){
    5. if( file.open( QIODevice::ReadOnly ) ){
    6. QTextStream stream( &file );
    7. contents = stream.readAll();
    8. }
    9. }
    10.  
    11. file.close();
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Readind textfiles

    Ok thx a lot,

    I will try it

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Readind textfiles

    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.

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Readind textfiles

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Readind textfiles

    Quote Originally Posted by LordQt View Post
    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.

    Quote Originally Posted by Valheru View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.