PDA

View Full Version : Question about method to read files using Qt.



robgeek
22nd March 2015, 23:29
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:

int line[ ] = new int[ 6 ];
for( String str: br.readLine().split( "\\s+" ) ) { //"\\s+" skips spaces/tabs
line = Integer.parseInt( str );
}

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!

jefftee
23rd March 2015, 00:57
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.