Why I can not get the string buffer length ?Help!!!

J

Joseph

Hi, all
I have a sample code like the following detailed:
//-----------------
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();
}
//-----------------------------
My question is : Why the value of i is 0, not the exact string
length? Could anybody tell me the root cause?

Thanks in advance!

Joseph
 
J

Joseph

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
 
J

Joseph

Well , I got the root cause , it is because the file I read from is a binary
format file!

Joseph

Joseph said:
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 said:
Hi, all
I have a sample code like the following detailed:
//-----------------
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();
}
//-----------------------------
My question is : Why the value of i is 0, not the exact string
length? Could anybody tell me the root cause?

Thanks in advance!

Joseph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top