Results 1 to 3 of 3

Thread: QT - reading line until ":"

  1. #1
    Join Date
    Jun 2017
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QT - reading line until ":"

    Hi,
    Could you tell me how to read line from txt file until sign ":"? I have file like this:

    mathematics: 5
    biology: 4.5
    IT: 5

    and I wanna read it to two others array. In c++ I use:

    getline(data, subject[i], ':');

    and it's working. I wanna do the same in QT but in subject[i]=in.readLine() I don't know if ther's something alike.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QT - reading line until ":"

    Use readLine() to read the whole line into a QString, then use QString::split() to get the two parts:

    Qt Code:
    1. QTextStream in( file ); // QFile * file
    2.  
    3. while ( !in.atEnd() )
    4. {
    5. QString line = in.readLine();
    6. QStringList fields = line.split( ": " );
    7. QString subject = fields[0];
    8. QString gpa = fields[1];
    9.  
    10. // Now do something with subject and gpa...
    11. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2017
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT - reading line until ":"

    thanks next time I'll use Code tags

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. Reading Binary Files From Asset folder using "fstream"
    By saad_saadi in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 24th June 2015, 09:20
  3. Replies: 3
    Last Post: 16th March 2015, 07:31
  4. Replies: 1
    Last Post: 3rd December 2013, 02:19
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

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.