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. QByteArray Remove not working?

QByteArray Remove not working?

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

    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 (!file.open(QIODevice::ReadOnly)){ // open file
    error.setText("Could not open file to read..."); // error?
    error.exec(); // exec
    }
    else{
    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...
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

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

        http://www.catb.org/~esr/faqs/smart-questions.html

        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