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. [SOLVED] How can I override a text line in txt file.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How can I override a text line in txt file.

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 4.9k 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.
  • N Offline
    N Offline
    Neutrox
    wrote on last edited by
    #1

    @ QFile file3(_filename3);
    QTextStream out3(&file3);
    if(!file3.open(QFile::WriteOnly)){
    qDebug() << "could not open file for writing";
    return;
    }else{
    QString item1 = ui->lineEdit_4->text();
    QString item2 = ui->lineEdit_5->text();
    out3 << item1 << "\r\n" ;
    out3 << item2 << "\r\n";
    out3 << _directory3;
    file3.flush();
    file3.close();
    }
    @

    I get three directories that appear in my lineEdits and write them in a text file.
    When I change some directory on my LineEdit eg _directory3, it should be written in my text file to replace the old. Only what happens the following problem: If I select the directory that is mair than the old (more characters), when you print the text file, it not replaces the old completely. For example:
    Old: C :/ Users / eng / Desktop / lineEditqt.txt
    New: C :/ Users / eng / Desktop / qt.txt.
    in my text file it is written:
    C :/ Users / eng / Desktop / qt.txtitqt.txt
    and not as it should be, it would be like this:
    C :/ Users / eng / Desktop / qt.txt

    How can I solve it?

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

      Hi,

      One question first: is that the only content of your 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
      0
      • N Offline
        N Offline
        Neutrox
        wrote on last edited by
        #3

        In my txt file has just 3 paths that I can override.

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

          Hii

          your given code is working right if i remove line 11.
          i have doubt on line 11
          What is this _directory3 ?
          is this same as item1 or item2 ?
          if yes How are you getting _directory3 ?

          Be Cute

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

            Then you should just read the file line by line in e.g. a QStringList. Replace the line you modified in the QStringList and then overwrite the content of the original file with the content of the QStringList.

            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
            • N Offline
              N Offline
              Neutrox
              wrote on last edited by
              #6

              Can you show me an example with this?

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

                Of reading a file line by line ?

                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
                • N Offline
                  N Offline
                  Neutrox
                  wrote on last edited by
                  #8

                  No. I don't now how to use QStringList in this case.

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

                    Then please, take some time to read the documentation of this class.
                    One way:
                    @QStringList lines
                    //open file etc.
                    while(!steam.atEnd()) {
                    lines << stram.readLine();
                    } @

                    There are also other way to do that e.g. using QString::split

                    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
                    • N Offline
                      N Offline
                      Neutrox
                      wrote on last edited by
                      #10

                      The solution was simpler than I imagined.
                      Simply put:
                      @if(!file3.open(QFile::ReadWrite | QFile::Truncate)){
                      qDebug() << "could not open file for writing";
                      return;@

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

                        With that you don't just change one line like you ask for, you are overwriting everything.

                        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
                        • N Offline
                          N Offline
                          Neutrox
                          wrote on last edited by
                          #12

                          Yes, but I also rewrite those who have not been changed.

                          1 Reply Last reply
                          0
                          • IamSumitI Offline
                            IamSumitI Offline
                            IamSumit
                            wrote on last edited by
                            #13

                            if your problem is solved then make this title as SOLVED by adding [SOLVED] tag before the title.

                            Be Cute

                            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