Run-Time Check Failure #2 - Stack around the variable 'obj1' was corrupted.
-
Can you show this code?
@
//code to add data to temp
@ -
The first problem I see the way you delete data.
it should be
@ delete[] data @Correct this, run and see if you get the error again.
-
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;@ -
@Class A
{
public:
char *data=NULL;
A()
{
}
@
BTW Line number 4 is an invalid C++ statement. Initialize data in A's constructor. -
Hi vijay
Thanks for reply.But still same error.
-
Hi situ
I have done it in constructor only.
Here by mistake i wrote like that. -
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'.
-
Hi Hunger
Thanks for the reply.
Can you just show me how to avoid loosing special characters or any other characters.
But the final buffer should be char*. -
Could you elaborate on why you need a char*? Also, what do you want to store in your char* (i.e. what encoding do you want)?
-
Hi
I want to read text files.I want data in char* because i am calling this function in win32 app. which do not know QByteArray.
-
That all depends on how your file is encoded... you need to decode it properly using the right text codec.