October 26, 2011

dvez43 dvez43
Ant Farmer
226 posts

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:

  1.         QFile file(globalfile);   // create file...
  2.         if (!file.open(QIODevice::ReadOnly)){   // open file
  3.             error.setText("Could not open file to read...");   // error?
  4.             error.exec();   // exec
  5.         }
  6.         else{                    
  7.             allfile = file.readAll();   // read line
  8.             processlocationstart = allfile.indexOf("configure");   // find "process udp"
  9.             processlocationend = allfile.indexOf("configure", processlocationstart+1);   // find the next "process"
  10.             processdifference = processlocationend - processlocationstart;  // find the difference
  11.             allfile.remove(processlocationstart, processdifference - 1);   // remove inbetween
  12.         }   // end else
  13.         file.close();    // close the file
  14.  
  15. // write to file...

2 replies

October 27, 2011

Mohsen Mohsen
Robot Herder
731 posts

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

 Signature 

www.madagon.com

October 27, 2011

Volker Volker
Robot Herder
5428 posts

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

 
  ‹‹ facing problem in use of QtConcurrent.      can’t read a whole xml file ››

You must log in to post a reply. Not a member yet? Register here!