数据库已经连接上了,现在想把数据库中的clob类型的数据读取出来并存放在一个CString类型的字符串中:
OClob oclob;
const char *hh="seqdesc";
dyn.GetFieldValue(hh,&oclob);
...... CString str; unsigned char *buffer=0;
try
{
fstream fs;
fs.open("descout.txt",ios::out);
fs.setmode(filebuf::binary);
size=oclob.GetSize();
// CString clobsize;
// clobsize.Format("%d",size);
// AfxMessageBox("clob size is:"+clobsize); unsigned long optchunk=oclob.GetOptimumChunkSize();
unsigned int bufsize=((int)(32768/optchunk))*optchunk;
if(bufsize>size)
bufsize=size;
buffer=(unsigned char*)malloc(bufsize);
// CString length;
// length.Format("%d",bufsize);
// AfxMessageBox("bufsize="+length); oclob.EnableStreaming(size);
short status=OLOB_NEED_DATA;
unsigned long amtread=0;
while(status==OLOB_NEED_DATA)
{ amtread=oclob.Read(&status,buffer,bufsize);
// CString desc=buffer;
//int n= desc.GetLength();
//CString length;
//length.Format("%d",n);
AfxMessageBox("buffer length="+length);
// AfxMessageBox(desc);
fs.write(buffer,amtread); str=str+desc;
int len=str.GetLength();
CString str1;
str1.Format("%d",len);
AfxMessageBox("str's length="+str1);
}
AfxMessageBox(str);
oclob.DisableStreaming();
fs.close();
}
catch(OException *E)
{
CString str;
str=E->GetErrorText();
AfxMessageBox(str,0,NULL);
}
if (buffer)
{
free(buffer);
} dyn.MoveNext(); // go to the next record
}
dyn.Close();
}catch(OException *e)
{
CString str;
str=e->GetErrorText();
AfxMessageBox(str,0,NULL);
// e->Cleanup();}
}
出现了这样的问题:
(1)写入文档descout.txt中的字符串是正确的
(2)str中的字符串出现了问题:
例如:一个字符串长度为158,经过测试:
clob size is:158
bufsize :158
buf length:162
显示第一次while循环的结果:
buf length:162
desc为79个正常字符+40个中文乱码+一个空位+‘?’
第二次循环结果为:
buf length:162
desc也为79个正常字符+乱码
str的总长度为324
比这个字符串的实际长度多了一倍多..而如果字符串的长度再长的话,就更夸张了哪位高手能帮帮我,是不是在CString和unsigned char*之间的转化上出了问题?oclob.Read(&status,buffer,bufsize);这条语句究竟是怎么执行的,即使bufsize=oclob.Getsize,依然要循环两次分别读取....
OClob oclob;
const char *hh="seqdesc";
dyn.GetFieldValue(hh,&oclob);
...... CString str; unsigned char *buffer=0;
try
{
fstream fs;
fs.open("descout.txt",ios::out);
fs.setmode(filebuf::binary);
size=oclob.GetSize();
// CString clobsize;
// clobsize.Format("%d",size);
// AfxMessageBox("clob size is:"+clobsize); unsigned long optchunk=oclob.GetOptimumChunkSize();
unsigned int bufsize=((int)(32768/optchunk))*optchunk;
if(bufsize>size)
bufsize=size;
buffer=(unsigned char*)malloc(bufsize);
// CString length;
// length.Format("%d",bufsize);
// AfxMessageBox("bufsize="+length); oclob.EnableStreaming(size);
short status=OLOB_NEED_DATA;
unsigned long amtread=0;
while(status==OLOB_NEED_DATA)
{ amtread=oclob.Read(&status,buffer,bufsize);
// CString desc=buffer;
//int n= desc.GetLength();
//CString length;
//length.Format("%d",n);
AfxMessageBox("buffer length="+length);
// AfxMessageBox(desc);
fs.write(buffer,amtread); str=str+desc;
int len=str.GetLength();
CString str1;
str1.Format("%d",len);
AfxMessageBox("str's length="+str1);
}
AfxMessageBox(str);
oclob.DisableStreaming();
fs.close();
}
catch(OException *E)
{
CString str;
str=E->GetErrorText();
AfxMessageBox(str,0,NULL);
}
if (buffer)
{
free(buffer);
} dyn.MoveNext(); // go to the next record
}
dyn.Close();
}catch(OException *e)
{
CString str;
str=e->GetErrorText();
AfxMessageBox(str,0,NULL);
// e->Cleanup();}
}
出现了这样的问题:
(1)写入文档descout.txt中的字符串是正确的
(2)str中的字符串出现了问题:
例如:一个字符串长度为158,经过测试:
clob size is:158
bufsize :158
buf length:162
显示第一次while循环的结果:
buf length:162
desc为79个正常字符+40个中文乱码+一个空位+‘?’
第二次循环结果为:
buf length:162
desc也为79个正常字符+乱码
str的总长度为324
比这个字符串的实际长度多了一倍多..而如果字符串的长度再长的话,就更夸张了哪位高手能帮帮我,是不是在CString和unsigned char*之间的转化上出了问题?oclob.Read(&status,buffer,bufsize);这条语句究竟是怎么执行的,即使bufsize=oclob.Getsize,依然要循环两次分别读取....
buffer=(unsigned char*)malloc(bufsize);
memset(buffer, 0, size);//2、循环次数是查询语句得到的记录数,有几条记录就会循环几次
//3、clob size,buffer size都是单条记录对应的字段长度。
buffer=(unsigned char*)malloc(bufsize);
menset(buffer,0 ,bufsize);//2、循环次数是查询语句得到的记录数,有几条记录就会循环几次
//3、clob size,buffer size都是单条记录对应的字段长度。
呵呵