Results 1 to 6 of 6

Thread: QTextStream

  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextStream

    I read a text file with QTextStream.
    1.If I read contents with readLine (),it is ok.
    2.Now I want to know how many rows first before I process the contents,so I use readALL(),and split it with "\n",I find that some text can not be splitted with "\n",so I try "\r\n" or "\r",and find it is still not ok.and the number of the splitting strings is always 1.
    How to do ?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextStream

    Show the code you are using to split the string you get back from readAll()

  3. #3
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextStream

    Qt Code:
    1. QFile file( fileName );
    2. if( !file.open( QIODevice::ReadOnly ) )
    3. {
    4. QMessageBox::warning(0,QObject::tr("Import SPS File"),QObject::tr("Can not open shot file:")+fileName);
    5. return false;
    6. }
    7. QTextStream stream( &file );
    8. QStringList allContents=stream.readAll().split("\n");
    To copy to clipboard, switch view to plain text mode 
    the elements number of allContents is always 1.

    By the way,when the text file is edited by wordpad,and split the contents of it with"\n",it is ok.

  4. #4
    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: QTextStream

    So why can't you use readLine()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextStream

    I need to know how many rows of the text file before I read line by line.

  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: QTextStream

    Quote Originally Posted by weixj2003ld View Post
    I need to know how many rows of the text file before I read line by line.
    And how is that different from reading the whole file at once using readAll()? Why can't you do this:

    Qt Code:
    1. while(!file.atEnd()) lines << stream.readLine();
    2. qDebug() << "I have" << lines.count() << "lines".
    3. doSomethingWith(lines);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTextStream
    By lahmitia in forum Qt Programming
    Replies: 3
    Last Post: 8th March 2011, 21:50
  2. QTextStream under Mac OS X
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 14th September 2009, 09:49
  3. QTextStream. getting a line?
    By afflictedd2 in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2009, 09:20
  4. QTextStream
    By WXNSNW in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2008, 16:43
  5. QTextStream help pls
    By munna in forum Newbie
    Replies: 5
    Last Post: 16th May 2006, 18:45

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.