CString->GetLength() does not work

  • Thread starter Thread starter Joseph
  • Start date Start date
J

Joseph

Hi, I have a question for CString. My C++ code is detailed as below,

FILE *fp1;
fp1=fopen("F:\\aa.dat","rb");
if(!feof(fp1))
{
// Step 1 :the following code is to read binary data from an
existing file
CString ss;
fread(ss.GetBuffer(1428),1428,1,fp1);

// Step 2: the following code is to test if the read action is
successful
fclose(fp1);
fp1 = fopen("F:\\bb.dat","wb+");
fwrite(ss.GetBuffer(),1428,1,fp1);
fclose(fp1);
// Step 3:the following code is to test the GetLength function!
int i;
i = ss.GetLength();
}
I find step 1 & 2 are all successful, but when I monitor the i value after
setp 3, I found the value is 0, could any body tell me why?

Thanks in advance!
Joseph
 
Joseph said:
I find step 1 & 2 are all successful, but when I monitor the i value after
setp 3, I found the value is 0, could any body tell me why?

When you're done writing to the string after GetBuffer, call
ReleaseBuffer (or use CStrBuf instead). Note that you need GetBuffer
only when you write to the CString, so here
fwrite(ss.GetBuffer(),1428,1,fp1);

where you only read the CString contents, GetBuffer is unnecessary.
 
Well, even I do not use fread to GetBuffer(),ie remove step 2 as I decribed
in my previous mail, I still can not get the CString length by GetLength()
method!

Joseph
 
Joseph said:
Well, even I do not use fread to GetBuffer(),ie remove step 2 as I decribed
in my previous mail, I still can not get the CString length by GetLength()
method!

And did you include the call to ReleaseBuffer?
 
Sorry , do I need to call the ReleaseBuffer() method before GetLength() , or
I misunderstood your meaning?

FILE *fp1;
fp1=fopen("F:\\aa.dat","rb");
if(!feof(fp1))
{
// Step 1 :the following code is to read binary data from an
existing file
CString ss;
fread(ss.GetBuffer(1428),1428,1,fp1);

// Step 2: the following code is to test if the read action is
successful
fclose(fp1);
fp1 = fopen("F:\\bb.dat","wb+");
fwrite(ss,1428,1,fp1);
fclose(fp1);
// Step 3:the following code is to test the GetLength function!
int i;
i = ss.GetLength();
}
I changed Step 2 by your request, do you mean I still need to add
ReleaseBuffer() call between step 2 & 3, or what else?

Joseph
 
Joseph said:
Sorry , do I need to call the ReleaseBuffer() method before GetLength() , or
I misunderstood your meaning?

From the documentation:

If you use the pointer returned by GetBuffer to change the string
contents, you must call ReleaseBuffer before using any other
CSimpleStringT member methods.
 
Joseph said:
Sorry , do I need to call the ReleaseBuffer() method before GetLength() , or
I misunderstood your meaning?

[snip]

Joseph:

Yes! Whenever you use GetBuffer you should call ReleaseBuffer.

David Wilkinson
 
Thanks David & Eberhard

I read the CString documents and I changed my code as below, but it still
does not work! I do not know why!

Line1: char *p1 = new char[1428];
Line2: fread(p1,1428,1,fp1);
Line3: CString str1(p1);
Line4: str1.ReleaseBuffer();
Line5: int i;
Line6: i = str1.GetLength();

// I found the value for i is 1 ,not 1428. Why?

but if I change line 2 to the following line ,it can work properly, why?

strcpy(p1,"ABCDEFG"); //not fread(p1,1428,1,fp1);


Could anybody give me a help?Help?

Thanks in advance!

Joseph


David Wilkinson said:
Joseph said:
Sorry , do I need to call the ReleaseBuffer() method before GetLength() ,
or I misunderstood your meaning?

[snip]

Joseph:

Yes! Whenever you use GetBuffer you should call ReleaseBuffer.

David Wilkinson
 
Well , I got the root cause , it is because the file I read from is a binary
format file, thanks to you all!

Joseph

Joseph said:
Thanks David & Eberhard

I read the CString documents and I changed my code as below, but it still
does not work! I do not know why!

Line1: char *p1 = new char[1428];
Line2: fread(p1,1428,1,fp1);
Line3: CString str1(p1);
Line4: str1.ReleaseBuffer();
Line5: int i;
Line6: i = str1.GetLength();

// I found the value for i is 1 ,not 1428. Why?

but if I change line 2 to the following line ,it can work properly, why?

strcpy(p1,"ABCDEFG"); //not fread(p1,1428,1,fp1);


Could anybody give me a help?Help?

Thanks in advance!

Joseph


David Wilkinson said:
Joseph said:
Sorry , do I need to call the ReleaseBuffer() method before GetLength()
, or I misunderstood your meaning?

[snip]

Joseph:

Yes! Whenever you use GetBuffer you should call ReleaseBuffer.

David Wilkinson
 
Joseph said:
Thanks David & Eberhard

I read the CString documents and I changed my code as below, but it still
does not work! I do not know why!

Line1: char *p1 = new char[1428];
Line2: fread(p1,1428,1,fp1);
Line3: CString str1(p1);
Line4: str1.ReleaseBuffer();
Line5: int i;
Line6: i = str1.GetLength();

// I found the value for i is 1 ,not 1428. Why?

but if I change line 2 to the following line ,it can work properly, why?

strcpy(p1,"ABCDEFG"); //not fread(p1,1428,1,fp1);


Could anybody give me a help?Help?

Thanks in advance!

Joseph


Joseph wrote:

Sorry , do I need to call the ReleaseBuffer() method before GetLength() ,
or I misunderstood your meaning?

[snip]

Joseph:

Yes! Whenever you use GetBuffer you should call ReleaseBuffer.

David Wilkinson

Joseph:

1. You only need ReleaseBuffer() if you have called GetBuffer().

2. You should not be using "new" here. Never new unless you have to.

Line1: char p1[1428];
Line2: fread(p1, 1428, 1, fp1);
Line3: CString str1(p1);
Line4: int i = str1.GetLength();

or

Line1: CString str1;
Line2: char* p1 = str1.GetBuffer(1428);
Line3: fread(p1, 1428, 1, fp1);
Line4: str1.ReleaseBuffer();
Line5: int i = str1.GetLength();

or

Line1: char p1[1428];
Line2: fread(p1, 1428, 1, fp1);
Line3: int i = strlen(p1);

But, as you seem to have noticed, all these codes depend on the
existence of a null character in the string.

David Wilkinson
 
Back
Top