数据库已经连接上了,现在想把数据库中的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,依然要循环两次分别读取....
  

解决方案 »

  1.   

    //1、分配内存之后,请初始化
    buffer=(unsigned char*)malloc(bufsize); 
    memset(buffer, 0, size);//2、循环次数是查询语句得到的记录数,有几条记录就会循环几次
    //3、clob size,buffer size都是单条记录对应的字段长度。
      

  2.   

    以此为准 前面有错~//1、分配内存之后,请初始化
    buffer=(unsigned char*)malloc(bufsize); 
    menset(buffer,0 ,bufsize);//2、循环次数是查询语句得到的记录数,有几条记录就会循环几次
    //3、clob size,buffer size都是单条记录对应的字段长度。
      

  3.   

    不对啊,我只查询了一条记录,但是它也循环了几次才完成..clobsize 和bufsize是我做测试用的.是想看看这一次查询的字符串长度有多少,而用的buffer又有多长...奇怪的是,一进入循环,buffer的长度就多了4,而这一条记录也要循环至少两次才能装载完成..
      

  4.   

    我自己弄懂了amtread返回了一次读取的字符串长度,我在追加的时候只截取那一部分有效的加到字符串里就行咯...
    呵呵