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. Help with QTextstream splitting "lang=2"
Forum Updated to NodeBB v4.3 + New Features

Help with QTextstream splitting "lang=2"

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 614 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.
  • Q Offline
    Q Offline
    qAlexKo
    wrote on 28 Oct 2021, 06:25 last edited by qAlexKo
    #1

    #include <QCoreApplication>
    #include <QString>
    #include <QTextStream>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QString qstr = "lang=2";  //! no spaces between lang and 2!)
    
    QTextStream ss(&qstr);
    QString lang; int kol_lang = 0;
    //ss >> lang >> dec >> kol_lang;  //it works if there are spaces between lang and 2
    
    //the following line is wrong - help!
    ss >> lang >> qSetPadChar('=') >> dec >> kol_lang;
    
    //separate setPadchar doesn't work
    

    //I want to use the stream technique - I know about a QString split possibility

    return a.exec();
    

    }

    J Q 2 Replies Last reply 28 Oct 2021, 06:28
    0
    • Q qAlexKo
      28 Oct 2021, 06:25

      #include <QCoreApplication>
      #include <QString>
      #include <QTextStream>

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      QString qstr = "lang=2";  //! no spaces between lang and 2!)
      
      QTextStream ss(&qstr);
      QString lang; int kol_lang = 0;
      //ss >> lang >> dec >> kol_lang;  //it works if there are spaces between lang and 2
      
      //the following line is wrong - help!
      ss >> lang >> qSetPadChar('=') >> dec >> kol_lang;
      
      //separate setPadchar doesn't work
      

      //I want to use the stream technique - I know about a QString split possibility

      return a.exec();
      

      }

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Oct 2021, 06:28 last edited by
      #2

      @qAlexKo Does

      ss >> lang >> qSetPadChar('=') >> kol_lang;
      

      work?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply 28 Oct 2021, 07:08
      0
      • Q qAlexKo
        28 Oct 2021, 06:25

        #include <QCoreApplication>
        #include <QString>
        #include <QTextStream>

        int main(int argc, char *argv[])
        {
        QCoreApplication a(argc, argv);

        QString qstr = "lang=2";  //! no spaces between lang and 2!)
        
        QTextStream ss(&qstr);
        QString lang; int kol_lang = 0;
        //ss >> lang >> dec >> kol_lang;  //it works if there are spaces between lang and 2
        
        //the following line is wrong - help!
        ss >> lang >> qSetPadChar('=') >> dec >> kol_lang;
        
        //separate setPadchar doesn't work
        

        //I want to use the stream technique - I know about a QString split possibility

        return a.exec();
        

        }

        Q Offline
        Q Offline
        qAlexKo
        wrote on 28 Oct 2021, 06:38 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • J jsulm
          28 Oct 2021, 06:28

          @qAlexKo Does

          ss >> lang >> qSetPadChar('=') >> kol_lang;
          

          work?

          Q Offline
          Q Offline
          qAlexKo
          wrote on 28 Oct 2021, 07:08 last edited by
          #4

          @jsulm
          ss >> lang >> qSetPadChar('=') >> kol_lang;
          doesn't work
          alt text

          J 1 Reply Last reply 28 Oct 2021, 07:13
          0
          • Q qAlexKo
            28 Oct 2021, 07:08

            @jsulm
            ss >> lang >> qSetPadChar('=') >> kol_lang;
            doesn't work
            alt text

            J Offline
            J Offline
            JonB
            wrote on 28 Oct 2021, 07:13 last edited by JonB
            #5

            @qAlexKo
            I don't know what you are trying to achieve with your use of qSetPadChar(), but it is only for use (and makes sense) with the << operator, not the >> operator. Same btw for setPadChar().

            Q 1 Reply Last reply 28 Oct 2021, 07:19
            1
            • J JonB
              28 Oct 2021, 07:13

              @qAlexKo
              I don't know what you are trying to achieve with your use of qSetPadChar(), but it is only for use (and makes sense) with the << operator, not the >> operator. Same btw for setPadChar().

              Q Offline
              Q Offline
              qAlexKo
              wrote on 28 Oct 2021, 07:19 last edited by qAlexKo
              #6

              @JonB, I want to achieve an easy thing -- to split the text "lang=2" using the stream techinique, without sscanf. Do the C++ text streams not allow to do such things? ;-) I can't believe it. Of course I don't insist on qSetPadChar -- just tell me how to split the "lang=2" that came from the text stream.

              J 1 Reply Last reply 28 Oct 2021, 07:31
              0
              • Q qAlexKo
                28 Oct 2021, 07:19

                @JonB, I want to achieve an easy thing -- to split the text "lang=2" using the stream techinique, without sscanf. Do the C++ text streams not allow to do such things? ;-) I can't believe it. Of course I don't insist on qSetPadChar -- just tell me how to split the "lang=2" that came from the text stream.

                J Offline
                J Offline
                JonB
                wrote on 28 Oct 2021, 07:31 last edited by
                #7

                @qAlexKo said in Help with QTextstream splitting "lang=2":

                I can't believe it.

                Well that's up to you :) The simple answer is that you cannot do this "splitting on input" via QTextStream (it will only split input on whitespace, and the "pad char" is only for output). Or, to be more precise, not without subclassing it and writing your own override for >> to do what you want. I refer you to e.g. https://stackoverflow.com/questions/27838186/qtextstream-read-a-string-until-tab.

                Using plain QTextStream you will need to use readAll() or readLine() and then QString::split('=') for your purpose.

                Q 1 Reply Last reply 28 Oct 2021, 07:50
                2
                • J JonB
                  28 Oct 2021, 07:31

                  @qAlexKo said in Help with QTextstream splitting "lang=2":

                  I can't believe it.

                  Well that's up to you :) The simple answer is that you cannot do this "splitting on input" via QTextStream (it will only split input on whitespace, and the "pad char" is only for output). Or, to be more precise, not without subclassing it and writing your own override for >> to do what you want. I refer you to e.g. https://stackoverflow.com/questions/27838186/qtextstream-read-a-string-until-tab.

                  Using plain QTextStream you will need to use readAll() or readLine() and then QString::split('=') for your purpose.

                  Q Offline
                  Q Offline
                  qAlexKo
                  wrote on 28 Oct 2021, 07:50 last edited by
                  #8

                  @JonB said in Help with QTextstream splitting "lang=2":

                  https://stackoverflow.com/questions/27838186/qtextstream-read-a-string-until-tab.

                  ...
                  class MyTextStream : public QTextStream {...
                  I like it.

                  Thank you

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qAlexKo
                    wrote on 28 Oct 2021, 08:41 last edited by qAlexKo
                    #9

                    It works like this:

                    class MyTextStream : public QTextStream {
                    public:
                    QChar pad, skip;
                    MyTextStream(QString* str, QChar _pad, QChar _skip) : QTextStream(str) { pad = _pad; skip = _skip; }

                    MyTextStream& operator>>(QString & str) {
                    QChar ch;
                    while (!atEnd()) {
                    QTextStream::operator >>(ch);
                    if (ch == skip) continue;
                    if (ch == pad) {
                    break;
                    }
                    str = str + ch;
                    }
                    return *this;
                    }
                    };

                    int main(int argc, char *argv[])
                    {
                    QCoreApplication a(argc, argv);

                    QString qstr = "lang=2";  //! no spaces between lanf and 2!)
                    
                    MyTextStream ss(&qstr, '=', ' ');
                    QString lang; int kol_lang = 0;
                    ss >> lang >> dec >> kol_lang;
                    
                    return a.exec();
                    

                    }

                    J 1 Reply Last reply 28 Oct 2021, 08:53
                    0
                    • Q qAlexKo
                      28 Oct 2021, 08:41

                      It works like this:

                      class MyTextStream : public QTextStream {
                      public:
                      QChar pad, skip;
                      MyTextStream(QString* str, QChar _pad, QChar _skip) : QTextStream(str) { pad = _pad; skip = _skip; }

                      MyTextStream& operator>>(QString & str) {
                      QChar ch;
                      while (!atEnd()) {
                      QTextStream::operator >>(ch);
                      if (ch == skip) continue;
                      if (ch == pad) {
                      break;
                      }
                      str = str + ch;
                      }
                      return *this;
                      }
                      };

                      int main(int argc, char *argv[])
                      {
                      QCoreApplication a(argc, argv);

                      QString qstr = "lang=2";  //! no spaces between lanf and 2!)
                      
                      MyTextStream ss(&qstr, '=', ' ');
                      QString lang; int kol_lang = 0;
                      ss >> lang >> dec >> kol_lang;
                      
                      return a.exec();
                      

                      }

                      J Offline
                      J Offline
                      JonB
                      wrote on 28 Oct 2021, 08:53 last edited by
                      #10

                      @qAlexKo
                      I have not tested, and you may say it does not apply to you because you will only ever have one line (in which case the whole overhead of using a QTextStream seems quite pointless to me), but what does your new code do now on a file like, say:

                      var1=value1
                      var2=value2
                      

                      ?

                      Q 1 Reply Last reply 29 Oct 2021, 10:45
                      0
                      • J JonB
                        28 Oct 2021, 08:53

                        @qAlexKo
                        I have not tested, and you may say it does not apply to you because you will only ever have one line (in which case the whole overhead of using a QTextStream seems quite pointless to me), but what does your new code do now on a file like, say:

                        var1=value1
                        var2=value2
                        

                        ?

                        Q Offline
                        Q Offline
                        qAlexKo
                        wrote on 29 Oct 2021, 10:45 last edited by qAlexKo
                        #11

                        @JonB said in Help with QTextstream splitting "lang=2":

                        I have not tested, and you may say it does not apply to you because you will only ever have one line (in which case the whole overhead of using a QTextStream seems quite pointless to me), but what does your new code do now on a file like, say:
                        var1=value1
                        var2=value2

                        MyTextStream is good for splitting a single text line only - it is the replacement for the unsafe sscanf -- you split the line and put values according their types.
                        var1=value1 is a text line that could be split in two QStrings var and value1.
                        but if you have var1=15.5 you can put 15.5 directly into the float var.

                        QString qstr = "lang=2.23"; 
                        MyTextStream ss(&qstr, '=', ' ');
                        QString lang; double kol_lang = 0;
                        ss >> lang >> fixed >> kol_lang;
                        
                        QString qstr = "var1=value1";
                        MyTextStream ss(&qstr, '=', ' ');
                        QString lang, qval;
                        ss >> lang >> qval;
                        
                        1 Reply Last reply
                        0

                        1/11

                        28 Oct 2021, 06:25

                        • Login

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