Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Code to return a particular number of line along with the text from a document in qt
Forum Updated to NodeBB v4.3 + New Features

Code to return a particular number of line along with the text from a document in qt

Scheduled Pinned Locked Moved Unsolved The Lounge
8 Posts 3 Posters 1.8k Views 1 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.
  • J Offline
    J Offline
    Joel George
    wrote on last edited by
    #1

    We are trying to code a program that will check for certain rules and find out violations of those rules from a text file. What we need help is with how to find in what line number the rule has been violated. How to return the line number of that particular line in which the rule has been violated, along with that text ?

    raven-worxR 1 Reply Last reply
    0
    • J Joel George

      We are trying to code a program that will check for certain rules and find out violations of those rules from a text file. What we need help is with how to find in what line number the rule has been violated. How to return the line number of that particular line in which the rule has been violated, along with that text ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Joel-George
      i don't know how anybody here should help you, since this heavily depends on your implementation of your rule-violation-checking?!
      You should be more precise with your question regarding Qt mechanics/classes i guess.

      Show the code where you read in the lines for example.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Joel George
        wrote on last edited by
        #3

        @raven-worx Sure.. this is the coding... Can you please tell how to print in the GUI the line number along with the content from that, the rule is violated.

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QTextStream>
        #include <QMessageBox>
        #include <QFileDialog>
        #include <QDebug>

        MainWindow::MainWindow(QWidget* parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow) {
        ui->setupUi(this);
        }

        MainWindow::~MainWindow() {
        delete ui;
        }

        #include <QFile>
        void CompareRules(QString OnePara ) {
        //QString a="//&&hai";
        // check the rules
        if (OnePara.contains("//"))
        QMessageBox::warning(NULL, ("My Application"), ("ERROR.\n""Rule Violated!" ));

        }

        void MainWindow::on_pushButton_clicked()
        {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Opentext File"),
        QDir::currentPath(),
        tr("text Files (*.txt)"));
        if (fileName.isEmpty()) {
        return;
        }

        QFile inputFile(fileName);
        QString Para;
        if (inputFile.open(QIODevice::ReadOnly)) {
          QTextStream in(&inputFile);
          while (!in.atEnd()) {\
            QString line = in.readLine();
            Para += line;
            qDebug()<<line;
            qDebug()<<Para;
            if (line!=NULL) { // we found blank line, so test and clear
                    qDebug()<<line;
              CompareRules(Para); //check
              qDebug() << "ParaRead: " << Para;
              //Para = ""; //clear
            }
          }
          inputFile.close();
        }
        

        }

        raven-worxR 1 Reply Last reply
        0
        • J Joel George

          @raven-worx Sure.. this is the coding... Can you please tell how to print in the GUI the line number along with the content from that, the rule is violated.

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QTextStream>
          #include <QMessageBox>
          #include <QFileDialog>
          #include <QDebug>

          MainWindow::MainWindow(QWidget* parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow) {
          ui->setupUi(this);
          }

          MainWindow::~MainWindow() {
          delete ui;
          }

          #include <QFile>
          void CompareRules(QString OnePara ) {
          //QString a="//&&hai";
          // check the rules
          if (OnePara.contains("//"))
          QMessageBox::warning(NULL, ("My Application"), ("ERROR.\n""Rule Violated!" ));

          }

          void MainWindow::on_pushButton_clicked()
          {
          QString fileName = QFileDialog::getOpenFileName(this, tr("Opentext File"),
          QDir::currentPath(),
          tr("text Files (*.txt)"));
          if (fileName.isEmpty()) {
          return;
          }

          QFile inputFile(fileName);
          QString Para;
          if (inputFile.open(QIODevice::ReadOnly)) {
            QTextStream in(&inputFile);
            while (!in.atEnd()) {\
              QString line = in.readLine();
              Para += line;
              qDebug()<<line;
              qDebug()<<Para;
              if (line!=NULL) { // we found blank line, so test and clear
                      qDebug()<<line;
                CompareRules(Para); //check
                qDebug() << "ParaRead: " << Para;
                //Para = ""; //clear
              }
            }
            inputFile.close();
          }
          

          }

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Joel-George
          simply use a counting variable:

          int lineCount = 1;
          while (!in.atEnd()) {
          // read line
          qDebug() << lineCount << line;
          ++lineCount; // increase count at the very end of the loop
          }
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • J Offline
            J Offline
            Joel George
            wrote on last edited by
            #5

            @raven-worx The current text file has content saying
            Let the Input File be s this:
            Hi
            //
            bye....

            Expected Output:

            Line 2: ERROR Rule Violated!

            raven-worxR 1 Reply Last reply
            0
            • J Joel George

              @raven-worx The current text file has content saying
              Let the Input File be s this:
              Hi
              //
              bye....

              Expected Output:

              Line 2: ERROR Rule Violated!

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @Joel-George
              is this a question? :)
              Or a statement that it is working?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              J 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @Joel-George
                is this a question? :)
                Or a statement that it is working?

                J Offline
                J Offline
                Joel George
                wrote on last edited by
                #7

                The above post was an example of how the output is expected and it was a question :D In the console this line count logic works. Can you help us with printing the same result in GUI ?

                1 Reply Last reply
                0
                • Yves MaurischatY Offline
                  Yves MaurischatY Offline
                  Yves Maurischat
                  wrote on last edited by Yves Maurischat
                  #8

                  First, change your rule checking method to return a boolean value instead of displaying the message box directly:

                  bool CompareRules(QString OnePara ) { 
                      if (OnePara.contains("//"))
                            return false;
                  
                      return true;
                  }
                  

                  Then add a counter variable and check the result of the rule checking method:

                  if (inputFile.open(QIODevice::ReadOnly)) {
                    QTextStream in(&inputFile);
                    int counter = 0;
                    while (!in.atEnd()) {
                      counter++;
                      QString line = in.readLine();
                      Para += line;
                      if (line!=NULL) {
                          if (!CompareRules(Para))  {
                              QMessageBox::warning(NULL, "My Application", QString("ERROR.\nRule Violated in line %1!").arg(counter) );
                              return;
                          }
                      }
                  
                  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