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) Read everything after an specific keyword

(SOLVED) Read everything after an specific keyword

Scheduled Pinned Locked Moved General and Desktop
qstringfileqfilereadqt4qt4.8
16 Posts 3 Posters 10.9k 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.
  • J jjan

    I fixed it, here is the solution!

    QFile rootFile(*p_path + "/Project.arden");
    				if (rootFile.open(QIODevice::ReadOnly))
    				{
    					QTextStream in(&rootFile);
    					while (!in.atEnd())
    					{
    						QString line = in.readAll();
    						/* lets get the track name */
    						QString trackName("name =");
    						int pos = line.indexOf(trackName);
    						QString theTrackName = line.right(pos + trackName.length());
    						printf(theTrackName.toStdString().c_str());
    					}
    				}
    
    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #7

    @jjan
    I guess you have changed also this line to something else

            QString trackName("name = ");
    

    because your file does not contain any "name =" and this cannot be found then.

    ALso you are mixing c and c++ which is possible, but not the prefered way. With Qt typically one uses QDebug for a fast debugging output.

    #include <QDebug>
    
    QFile rootFile(*p_path + "/Project.arden");
    if (rootFile.open(QIODevice::ReadOnly))
    {
          QTextStream in(&rootFile);
          while (!in.atEnd())
          {
                QString line = in.readAll();
               /* lets get the track name */
                QString trackName("name =");
                int pos = line.indexOf(trackName);
                QString theTrackName = line.right(pos + trackName.length());
                // printf(theTrackName.toStdString().c_str());
                qDebug() << theTrackName;
          }
    }
    

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jjan
      wrote on last edited by
      #8

      @koahnig

      The problem now is that it returns not that what I want. However when I change it to name = it gives me everything what behind author = is. Its a bit confusing :/

      K 1 Reply Last reply
      0
      • J jjan

        @koahnig

        The problem now is that it returns not that what I want. However when I change it to name = it gives me everything what behind author = is. Its a bit confusing :/

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #9

        @jjan
        ???

        If you have "name =" the indexOf is exactly searching search for "name =" it cannot match anythingelse. It looks exactly for the same sequence of characters. Also the blanks have to match exactly. Your file contains "spec =", "includes =", "title =" and "author =". Therefore, you have to use those strings for searching. With QRegExp you can gain more flexibility of course. However, this is typically a bit confusing in the beginnng.

        Vote the answer(s) that helped you to solve your issue(s)

        J 1 Reply Last reply
        0
        • K koahnig

          @jjan
          ???

          If you have "name =" the indexOf is exactly searching search for "name =" it cannot match anythingelse. It looks exactly for the same sequence of characters. Also the blanks have to match exactly. Your file contains "spec =", "includes =", "title =" and "author =". Therefore, you have to use those strings for searching. With QRegExp you can gain more flexibility of course. However, this is typically a bit confusing in the beginnng.

          J Offline
          J Offline
          jjan
          wrote on last edited by jjan
          #10

          @koahnig

          What I get is:

           untitled
          author = none
          

          With code:

          QString trackName("title =");
          				            int pos = line.indexOf(trackName);
          				            if (pos >= 0)
          				            {
          					            QString theTrackName = line.mid(pos + trackName.length());
          					            qDebug() << theTrackName;
          				        	}
          
          K 1 Reply Last reply
          0
          • J jjan

            @koahnig

            What I get is:

             untitled
            author = none
            

            With code:

            QString trackName("title =");
            				            int pos = line.indexOf(trackName);
            				            if (pos >= 0)
            				            {
            					            QString theTrackName = line.mid(pos + trackName.length());
            					            qDebug() << theTrackName;
            				        	}
            
            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #11

            @jjan
            This is completely correct with what you wrote in your program.

            QString line = in.readAll();
            

            reads the complete file into the string called line. You are searching for the start of text of "title =" in this complete string. You get a position greater than 0 because the string somewhere later.

            QString theTrackName = line.mid(pos + trackName.length());
            

            will copy the content starting after pos (begin of presence of search string) plus the length of search string. Therefore theTrackName will contain " untitled" followed by the remainder of the content.

            You can replace your readAll (reading the complete file into the string at once) with readLine.
            This should work for your case also.

            When you use Qt creator or another IDE you should use the debugger. This allows you normally to go through the program step by step.
            Further it allows you also to inspect the different vairiables and their actual content after each line.

            Vote the answer(s) that helped you to solve your issue(s)

            J 2 Replies Last reply
            1
            • K koahnig

              @jjan
              This is completely correct with what you wrote in your program.

              QString line = in.readAll();
              

              reads the complete file into the string called line. You are searching for the start of text of "title =" in this complete string. You get a position greater than 0 because the string somewhere later.

              QString theTrackName = line.mid(pos + trackName.length());
              

              will copy the content starting after pos (begin of presence of search string) plus the length of search string. Therefore theTrackName will contain " untitled" followed by the remainder of the content.

              You can replace your readAll (reading the complete file into the string at once) with readLine.
              This should work for your case also.

              When you use Qt creator or another IDE you should use the debugger. This allows you normally to go through the program step by step.
              Further it allows you also to inspect the different vairiables and their actual content after each line.

              J Offline
              J Offline
              jjan
              wrote on last edited by
              #12

              @koahnig

              This worked, many thanks!

              1 Reply Last reply
              0
              • T Offline
                T Offline
                t3685
                wrote on last edited by
                #13

                Hi,

                Is the file you are reading in some sort of settings ini file? If yes, you can use the QSettings class to make things easier and less error-prone.

                Greetings,

                t3685

                J 1 Reply Last reply
                0
                • T t3685

                  Hi,

                  Is the file you are reading in some sort of settings ini file? If yes, you can use the QSettings class to make things easier and less error-prone.

                  Greetings,

                  t3685

                  J Offline
                  J Offline
                  jjan
                  wrote on last edited by
                  #14

                  @t3685

                  It looks a little bit like an INI but has different style etc, so no its not a settings file ;) But thanks for the info!

                  1 Reply Last reply
                  0
                  • K koahnig

                    @jjan
                    This is completely correct with what you wrote in your program.

                    QString line = in.readAll();
                    

                    reads the complete file into the string called line. You are searching for the start of text of "title =" in this complete string. You get a position greater than 0 because the string somewhere later.

                    QString theTrackName = line.mid(pos + trackName.length());
                    

                    will copy the content starting after pos (begin of presence of search string) plus the length of search string. Therefore theTrackName will contain " untitled" followed by the remainder of the content.

                    You can replace your readAll (reading the complete file into the string at once) with readLine.
                    This should work for your case also.

                    When you use Qt creator or another IDE you should use the debugger. This allows you normally to go through the program step by step.
                    Further it allows you also to inspect the different vairiables and their actual content after each line.

                    J Offline
                    J Offline
                    jjan
                    wrote on last edited by jjan
                    #15

                    @koahnig
                    Sorry that I am posting here again. My file looks like this:

                    window_color_red = 130

                    The code to get this is:

                    void
                    		Theme::loadTheme(QPalette& palette, QApplication& app, Radon::Ui::MainWindow *window)
                    		{
                    			QString themePath = getTheme();
                    			printf(themePath.toStdString().c_str());
                    			QFile t(themePath);
                    			if (t.open(QIODevice::ReadOnly))
                    			{
                    				QTextStream input(&t);
                    				while (!input.atEnd())
                    				{
                    					QString line = input.readLine();
                    					QString windowRedColorKeyword("window_color_red = ");
                    					int redPos = line.indexOf(windowRedColorKeyword);
                    					if (redPos >= 0)
                    					{
                    						QString windowRedColor = line.mid(redPos + windowRedColorKeyword.length());
                    						printf(windowRedColor.toStdString().c_str());
                    					}
                    				}
                    			}
                    		}
                    

                    but it don't gives me the number 130, it gives me nothing. Is this basically because it only can read text and not numbers or why?

                    EDIT

                    This was as mistake I've made, forgot to remove the " in the path!

                    K 1 Reply Last reply
                    0
                    • J jjan

                      @koahnig
                      Sorry that I am posting here again. My file looks like this:

                      window_color_red = 130

                      The code to get this is:

                      void
                      		Theme::loadTheme(QPalette& palette, QApplication& app, Radon::Ui::MainWindow *window)
                      		{
                      			QString themePath = getTheme();
                      			printf(themePath.toStdString().c_str());
                      			QFile t(themePath);
                      			if (t.open(QIODevice::ReadOnly))
                      			{
                      				QTextStream input(&t);
                      				while (!input.atEnd())
                      				{
                      					QString line = input.readLine();
                      					QString windowRedColorKeyword("window_color_red = ");
                      					int redPos = line.indexOf(windowRedColorKeyword);
                      					if (redPos >= 0)
                      					{
                      						QString windowRedColor = line.mid(redPos + windowRedColorKeyword.length());
                      						printf(windowRedColor.toStdString().c_str());
                      					}
                      				}
                      			}
                      		}
                      

                      but it don't gives me the number 130, it gives me nothing. Is this basically because it only can read text and not numbers or why?

                      EDIT

                      This was as mistake I've made, forgot to remove the " in the path!

                      K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #16

                      @jjan
                      Just for an iteration the code reads line by line. As long as the search text is found, it will provide the remainder of the line. There is real dependency to the content as long as it is displayble (probably also some non-displayable characters). There are some special characters marking the end of lines (line feed (LF) and carriage return (CR) are those at least).

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      1

                      • Login

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