Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] ! Qt Array Problem (little one) read line by line
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 5.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrNoway
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      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();
      }@


      Code is poetry

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrNoway
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jake007
          wrote on last edited by
          #4

          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


          Code is poetry

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #5

            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!
            1 Reply Last reply
            0
            • M Offline
              M Offline
              MrNoway
              wrote on last edited by
              #6

              [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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved