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"
QtWS25 Last Chance

Help with QTextstream splitting "lang=2"

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 611 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 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();
    

    }

    jsulmJ Q 2 Replies Last reply
    0
    • Q qAlexKo

      #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();
      

      }

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • Q qAlexKo

        #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 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @qAlexKo Does

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

          work?

          Q Offline
          Q Offline
          qAlexKo
          wrote on last edited by
          #4

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

          JonBJ 1 Reply Last reply
          0
          • Q qAlexKo

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            1
            • JonBJ JonB

              @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 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.

              JonBJ 1 Reply Last reply
              0
              • Q qAlexKo

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                2
                • JonBJ JonB

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

                    }

                    JonBJ 1 Reply Last reply
                    0
                    • Q qAlexKo

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

                      }

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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
                      0
                      • JonBJ JonB

                        @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 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

                        • Login

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