Important: Please read the Qt Code of Conduct -

QByteArray Remove not working?

  • So here's the situation.

    I am reading in a text file into a QByteArray (which is successful) using readall(). At this point, what I am trying to accomplish is searching through the QByteArray for a specific string "configure something" (this is just an example).

    This is when I use indexof() to get the location of the string, next I want to search for the next instance of this line. So once again, I use indexof(), but I start it from 1+ the previous location to find the next instance of the object.

    Now once I have both index's, I want to remove inbetween them using remove(index of start, length). My index of start would be my first index found, and length would be the difference between both of the index's then write that back into the file to remove the section completely. Get my drift?

    Now for some reason, when I use the remove function for the QByteArray, It removes all of the contents of the QByteArray and I am left with nothing! So when I go to write to the file...there is nothing there. I stepped through and it indeed finds the first index and the second index.

    Any ideas?

    here's a code example:

    QFile file(globalfile); // create file...
    if (!{ // open file
    error.setText("Could not open file to read..."); // error?
    error.exec(); // exec
    allfile = file.readAll(); // read line
    processlocationstart = allfile.indexOf("configure"); // find "process udp"
    processlocationend = allfile.indexOf("configure", processlocationstart+1); // find the next "process"
    processdifference = processlocationend - processlocationstart; // find the difference
    allfile.remove(processlocationstart, processdifference - 1); // remove inbetween
    } // end else
    file.close(); // close the file

    // write to file...

  • use qDebug(allfile) to see if the problem is when writing to file or when you use remove function.

  • Additionally, check the values of your positions and difference whether they are sane.

Log in to reply