Run-Time Check Failure #2 - Stack around the variable 'obj1' was corrupted.
-
Hi Denis
@
opens the file to read
QString str = myfile.readAll();//Reads The full file
QByteArray ba = str.toLatin1();
int iByteArraySize = ba.size();
char *chrData = new char[iByteArraySize + 1];
strcpy(chrData,ba.data());
myfile.close();
return chrData;@ -
I don't see anything wrong with the code.. unless you are hiding some code from us :)
-
Why don't you use QByteArray in favor of char*? That is way safer to work with and should have very similar memory and run-time characteristics: It is basically just a char * that is guaranteed to end in '\0'.
@
QString str = myfile.readAll();//Reads The full file
QByteArray ba = str.toLatin1();
int iByteArraySize = ba.size();
char *chrData = new char[iByteArraySize + 1];
strcpy(chrData,ba.data());
@First line reads a the file in the form of a QByteArray into a UTF-16 encoded string, assuming the input to be ASCII encoded IIRC (but I might be wrong here in which case the default text codec will be used). This is lossy as all special characters, even those in the Latin1 range, are getting dropped or damaged by the recoding. Then you turn the UTF-16 encoded string back into a Latin1-encoded QByteArray (save since by now you have ASCII chars only and those are a subset of Latin1). Finally you copy that over into a char*. That is not really efficient: You got 4 copies (file to bytearray, bytearray to string, string to bytearray and bytearray to char*) and 2 format conversions (from 1byte per character to 2byte chars and back) going on here.
Plus the strcpy is not the safest operation around, even though it is fine here since QByteArray makes sure the string is terminated by '\0'.
-
That all depends on how your file is encoded... you need to decode it properly using the right text codec.