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. Problem with finding pattern using QFile[SOLVED]

Problem with finding pattern using QFile[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.9k Views 2 Watching
  • 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.
  • R Offline
    R Offline
    Roma_12
    wrote on last edited by Roma_12
    #1

    Hello everyone!

    I have faced with some trouble while using next program:

    QFile file1("/Path/To/File/input.txt");
    file1.open(QIODevice::ReadOnly);
    QString s = file1.readAll();
    
    if (!file1.isOpen()) {
        qDebug() << "Not";
        return -1;
    }
    
    file1.close();
    
    
    QRegExp reg(QString("%1").arg("Слово"));
    
    QLabel l;
    l.setText(reg.lastIndexIn(s));
    l.show();
    

    I am trying to find word in my file input.txt. There is only one word "Слово". But every time I getting result -1.

    But in next program:

    QString s = QByteArray("Слово");
    
    QRegExp reg(QString("%1").arg("Слово"));
    
    QLabel l;
    l.setText(reg.lastIndexIn(s));
    l.show();
    

    The result is 0.

    Why the results are different? And what I need to do to get correct answer in first program?
    Thank in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why not just use QString's contains method ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Why not just use QString's contains method ?

        R Offline
        R Offline
        Roma_12
        wrote on last edited by
        #3

        @SGaist Hello and thank you for answer.
        Thank for this method, but it doesn't help me with my problem.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What's the encoding of input.txt ?

          By the way QRegExp rx("Слово"); is enough to create your QRegExp.

          On a side note, if you are using Qt 5, QRegularExpression is recommended.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          0
          • SGaistS SGaist

            What's the encoding of input.txt ?

            By the way QRegExp rx("Слово"); is enough to create your QRegExp.

            On a side note, if you are using Qt 5, QRegularExpression is recommended.

            R Offline
            R Offline
            Roma_12
            wrote on last edited by Roma_12
            #5

            @SGaist The Unicode encoding.
            So I need to use QTextStream to solve my problem.
            Thank for your help!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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