Results 1 to 2 of 2

Thread: Question about method to read files using Qt.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Question about method to read files using Qt.

    Hello, guys!

    So, i have a file of thousands of lines containing only numbers from 01 to 99 and one space/tab between them, just like the following example:
    37 36 56 27 42 23
    05 25 12 10 60 24
    25 28 30 33 51 11
    ...
    I want to convert these numbers to "int" and put them in a array of int. In Java, to do that, i read line by line and i use "split( )" method which splits this string given a regular expression, like the following code:
    Qt Code:
    1. int line[ ] = new int[ 6 ];
    2. for( String str: br.readLine().split( "\\s+" ) ) { //"\\s+" skips spaces/tabs
    3. line = Integer.parseInt( str );
    4. }
    To copy to clipboard, switch view to plain text mode 

    I would like to know how can i do that using Qt libraries if is possible or how can i do that in C++.

    Thanks!

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Question about method to read files using Qt.

    You would basically do the same thing using the following Qt classes:


    • QFile to open/readLine from the file containing the lines of integers
    • QString::split to split the line at whitespace returning a QStringList
    • Iterate over the QStringList to get each string value and use QString::toInt() to convert to an integer
    • QVector to create an array of the resulting integers


    Good luck.

  3. The following user says thank you to jefftee for this useful post:

    robgeek (24th March 2015)

Similar Threads

  1. Replies: 6
    Last Post: 12th October 2015, 08:59
  2. QTcpSocket question, write method
    By Anslem in forum Newbie
    Replies: 3
    Last Post: 19th May 2014, 21:21
  3. Question about method named record() in QSqlTableModel
    By silentyears in forum Qt Programming
    Replies: 7
    Last Post: 21st January 2014, 09:52
  4. QGraphicsItem pos() method question!
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 19:56
  5. Replies: 12
    Last Post: 17th June 2009, 05:34

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.