View Full Version : Strange problem

18th December 2009, 09:08
Hello, i have a problem with writing to char** array from file.
I couldn't explain why, first n-1 lines is copied with no problem, but in last n line it outputs only last, and previous.


Example file :
1223 John
343 Dude
233 Zordo

I'm doing it becouse I want sort file by numbers in line beggining.
Maybe someone could give me some other way to do it in simply way?

18th December 2009, 09:34
I don't know how fast would that be, but you could try using
QMap<int, QString>

Read one line to QString, catch the number at the beginning of the line (using QRegExp for example) and use it as a key in QMap. Than simply iterate over your QMap.

Maybe Qt Gurus know better solution ;)

BTW next time paste your code to the post and use CODE tags.

18th December 2009, 09:53
char *tmp=(char*)(txt.readLine().toStdString().c_str()) ;

I'm not a C++ expert but I think you should not do things like this. c_str() will give you a pointer internal object's storage containing null terminated string.
I'm not sure if the pointer you keep in tab[0] will be still valid when you are on next iteration after another

Try what I wrote in a previous post - it's definitely more Qt-ish ;)

18th December 2009, 19:02
Is it a constraint of the assignment that you must use C-style strings and arrays? Neither is appropriate for this problem, unless that requirement has been imposed.

If you are not required to use C-style strings, use std::string or QString instead.

If you are not required to use arrays, use containers (either Standard Template Library containers or Qt containers (http://doc.trolltech.com/latest/containers.html)) instead.

If you must use an array of C-style strings, then calhal’s observation applies: when c_str() is applied to a temporary, the value it returns is guaranteed valid only during the statement that contains the call. (When applied to a non-temporary, it’s guaranteed valid only so long as the object to which it was applied exists and no non-const member function has been called on that object.) It’s only by chance that the code you have is working at all. Instead, you must save the QString or std::string, allocate a character buffer of the appropriate size (QString::length + 1), then copy the character data into the buffer (e.g., with strcpy). Don’t forget to delete the buffers in your clean-up code.

Much simpler and better, though, would be to use QStrings in containers.

18th December 2009, 21:46
#include <QString>
#include <QMap>
#include <QRegExp>
#include <QMapIterator>

int main () {

QFile file("../MOVE QWidget/gamefiles/hiScores.txt");
QMap<int,QString> fileMap;

if (file.open(QIODevice::ReadOnly)) {

QTextStream txt(&file);
QRegExp rx("(^[0-9]+)");

while ( !txt.atEnd() ) {
QString line =txt.readLine();
int number = rx.cap(1).toInt();

QMapIterator<int,QString> i(fileMap);
while ( i.hasPrevious()) {
std::cout<<i.key()<<" ";


Ok, thanks a lot for your help!
As you see, i go through first advice of calhal.
code snippet of course in 'CODE' tags ;-)