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. Edit text in File

Edit text in File

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.7k 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.
  • C Offline
    C Offline
    Caio.Sousa
    wrote on last edited by
    #1

    Hi everyone, I have one more question, how can I edit a text in a file?

    In the program records several information and would like to edit two that will be what the user type in a line edit.

    My code:

    
        QString valor = ui->edtRValor->text();
        QDate dateIn = ui->dateRInicial->date();
        QString dataInicio = dateIn.toString();
        QDate dateFi = ui->dateRFim->date();
        QString FimDate = dateFi.toString();
        
         QFile arch("C:\\Users\\Caio\\Documents\\cadastrarQuarto.txt");
             if(!sr.open(QIODevice::ReadOnly | QIODevice::Text))
                     return;
          QTextStream out(&arch);
    

    How do I get the value typed and swap with a value that is written in the file?
    My file:

    231-1º-40-sáb jan 1 2000-qua jan 5 2000-160-2
    441-4º-40-sáb jan 1 2020-qua jan 5 2200-190-8
    
    jsulmJ 1 Reply Last reply
    0
    • C Caio.Sousa

      Hi everyone, I have one more question, how can I edit a text in a file?

      In the program records several information and would like to edit two that will be what the user type in a line edit.

      My code:

      
          QString valor = ui->edtRValor->text();
          QDate dateIn = ui->dateRInicial->date();
          QString dataInicio = dateIn.toString();
          QDate dateFi = ui->dateRFim->date();
          QString FimDate = dateFi.toString();
          
           QFile arch("C:\\Users\\Caio\\Documents\\cadastrarQuarto.txt");
               if(!sr.open(QIODevice::ReadOnly | QIODevice::Text))
                       return;
            QTextStream out(&arch);
      

      How do I get the value typed and swap with a value that is written in the file?
      My file:

      231-1º-40-sáb jan 1 2000-qua jan 5 2000-160-2
      441-4º-40-sáb jan 1 2020-qua jan 5 2200-190-8
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Caio.Sousa said in Edit text in File:

      if(!sr.open(QIODevice::ReadOnly | QIODevice::Text))

      What is sr and why do you open it in read-only mode?

      "How do I get the value typed and swap with a value that is written in the file?" - you already get the typed values in the code above, right? I don't really understand your description. What do you really want to do? Do you want to append the text typed by the user to the end of the file or do you want to replace existing text with new (which text in the file exactly)?

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

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Caio.Sousa
        wrote on last edited by
        #3

        Sorry, I'm Brazilian and I do not speak much English.

        I want to attach the text typed by the user to one that already exists.

        For example:

        Caio-19 years-Player

        I will receive a text in an edit line and the file will be:

        Caio-40years-Student

        jsulmJ 1 Reply Last reply
        0
        • C Caio.Sousa

          Sorry, I'm Brazilian and I do not speak much English.

          I want to attach the text typed by the user to one that already exists.

          For example:

          Caio-19 years-Player

          I will receive a text in an edit line and the file will be:

          Caio-40years-Student

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Caio.Sousa You still did not explain what "sr" actually is. I assume you mean arch:

          QFile arch("C:\\Users\\Caio\\Documents\\cadastrarQuarto.txt");
          if(!arch.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
              return;
          QTextStream out(&arch);
          out << (valor + dateIn.toString() + dataInicio + dateFi.toString() + FimDate + '\n');
          

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

          1 Reply Last reply
          2
          • C Offline
            C Offline
            Caio.Sousa
            wrote on last edited by
            #5

            I want to search for certain text and edit in a file I will receive the text entered by the user and must be replaced by a certain text already existing in the file.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Caio.Sousa
              wrote on last edited by
              #6

              Example:

              Cat - Dog - 19 years.

              User typed fox
              I want to edit the dog for the fox

              New File:
              Cat - Fox - 19 years.

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

                Hi,

                Wouldn't it be simpler to do that using sed ?

                Otherwise, open the file, load it completly, use a regex for the search and replace part and then dump the content back to the file.

                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
                2
                • C Offline
                  C Offline
                  Caio.Sousa
                  wrote on last edited by
                  #8

                  I am new in QT Creator,
                  And I do not quite understand how you use regex and sed

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

                    sed is *nix command that allows you to parse and modify file content using regular expressions.

                    As for regular expression in Qt see QRegularExpression.

                    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
                    2

                    • Login

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