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] Problem with QFile class, please I need your help
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem with QFile class, please I need your help

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.5k 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.
  • R Offline
    R Offline
    rishardcarranza
    wrote on last edited by
    #1

    Please help me with my problem :
    I am trying to write in a text file with the QFile class of Qt, when I save the first time, no problem, but when I save the next time and the file exist, the file is blank, why is that ?... I attach the code ...

    @void frmAlumnos::ctrlFile(const QString &nomArc, QString idReg)
    {
    QFile ctrlFile(nomArc);
    if(ctrlFile.exists()){
    ctrlFile.open(QIODevice::Text | QIODevice::ReadOnly);
    QTextStream txtstr(&ctrlFile);
    QString sli = txtstr.readAll();
    QString sli2;
    QStringList cad = sli.split("\n");

        ctrlFile.flush();
        ctrlFile.close();
    
        //sli+=idReg+"\n";
    
        ctrlFile.open(QIODevice::Text | QIODevice::WriteOnly);
        if (!ctrlFile.isOpen()){
            return;
        }else{
        QTextStream txtstr1(&ctrlFile);
        int ctrol=0;
        for (int i=0;i<cad.size();i++){
            if (cad[i].isEmpty()){return;} // If find a blank space
            if (cad[i]==idReg){
                ctrol++;
            }
            sli2+=cad[i]+"\n";
        }
        if (ctrol<=0){
            sli2+=idReg+"\n";
        }
        txtstr1<<sli2;
        ctrlFile.flush();
        ctrlFile.close();
        }
    
    }else{
    ctrlFile.open(QIODevice::Text | QIODevice::WriteOnly);
    if(!ctrlFile.isOpen()){return;}
    QTextStream txtstr(&ctrlFile);
    txtstr<<idReg+"\n";
    ctrlFile.flush();
    ctrlFile.close();
    }
    

    }@

    I will be grateful to you

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      You open a file for write, which means that you delete the content of the file and then you return from the function without writing anything to the file.
      File is closed by QFile destructor and it is empty.

      See line 23 in your example.
      @
      if (cad[i].isEmpty()){return;} // If find a blank space
      @

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        You probably do not need to call QFile::flush().

        Maybe your code is hitting line 23 and returning before anything is written into the file? Or maybe you want to append to the file, in which case you need to add QFile::Append flag to QFile::open() call.

        (Z(:^

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rishardcarranza
          wrote on last edited by
          #4

          Heeeeeeeey !!!! really ? this the error on line 23, the @return@ code is wrong, the correct sintax is @continue@, because the @return@ is for exit the function... :-/... Very thanks sierdzio ... it's solution for my problem ...

          Regards

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rishardcarranza
            wrote on last edited by
            #5

            Heeeeeeeey !!!! really ? this the error on line 23, the @return@ code is wrong, the correct sintax is @continue@, because the @return@ is for exit the function... :-/... Very thanks andreyc ... it's solution for my problem ...

            Regards

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              I merged the duplicated threads.

              rishardcarra please do not duplicate your questions in the same language.

              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