[SOLVED] ! Qt Array Problem (little one) read line by line



  • I hope you genius can help me,
    i try to read a .txt file line by line and store it in a string array.

    the first line is saved fine, the next one dont.

    //IT's working, my mistake, see comment below

    @ QString key_storage[20];
    int i = 0;

      QString line;
         QString filename ="key.txt";
         QFile file(filename);
         if ( file.open(QIODevice::ReadOnly  | QIODevice::Text))
        {
            QTextStream in( &file );
                         line = in.readLine();
    

    key_storage[0] = line;
    while ( !line.isNull() )
    {
    line = in.readLine();
    key_storage[i+1] = line ;
    i++;

    }@

    EDITED: problem solved



  • Hi!

    Everything seems OK.

    Try this:
    @QFile file("key.txt");
    QStringList key_storage; // QStringList = QList<QString>

    if ( file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    key_storage = QString(file.readAll()).split('\n');

    file.close();
    }@



  • yea, TT god, just wasted 3hours trying to solve this shit >_<.

    It was actually working.

    my mistake was,
    I was just checking the variables in the debuger
    and it showed me all the time wrong values,

    actually i am used to work with the "break points"
    but with this array i must have understood something wrong.
    I used the "control variable option" TT or whatever it is called in English,

    so for everyone who wants to write the string line by line into another file. you can use this here.

    @ QFile file2("TTTTTT.txt");
    file2.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out2(&file2);

    int j = 0;
    while (j <=20)
    { out2 << key_storage[j] << "\n" ;
    j++;}
    @

    it mightbe useful for someone



  • I use qDebug() in such simple cases. It's usually faster.

    Please mark thread as solved ( edit first post and add [SOLVED] in front of a title).

    Regards,
    Jake



  • Few remarks:

    • Indent properly. Your indenting seems random.
    • Don't use plain C-Arrays (QString keyStorage [20]), rather use a QStringList here. Your program has serious issues, for example, it will crash when the file contains more than 20 lines.
    • Your output code accesses indices 0 to 20, but your array only has 0 to 19 indices. So this is a time bomb, too. Another argument not to use C-Arrays if you don't know that you absolutely need them (e.g. for high performance computing).
    • Style hint: Don't mix naming conventions. Either "keyStorage" and "fileName" or "key_storage" and "file_name". Since working in a Qt environment, you should even prefer the first (called camelCase) style. But in any case, don't mix!


  • [quote author="Jake007" date="1354445141"]I use qDebug() in such simple cases. It's usually faster.

    Please mark thread as solved ( edit first post and add [SOLVED] in front of a title).

    Regards,
    Jake[/quote]

    I just did as you say :P


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.