返回值--CString和指针的区别

CString 对string进行了重新的封装处理

char *ReadLob(CString filename)

{

  char msgbody[1024];

  return msgbody

}

//////////////////////////////////////////////////////////////////////////////////////////////////////<区别

int ReadLob(CString filename, char* node702_msgbody, int iSz)

  std::ifstream file;  file.open(filename); 

  if(!file)

   {   printf("Unable to open file\n");   return -1;  }

   char pBuf[1024*4];  int iRead = 0, iCur = 0;

  while(!file.eof())    

  {       

      file.read(pBuf,sizeof(pBuf));      

       iRead = file.gcount();            

      int iCpy = iRead+iCur >= iSz ? iSz - iCur - 1 : iRead;       

      strncpy(node702_msgbody+iCur, pBuf, iCpy);       iCur += iCpy;      

       if(iCur == sizeof(node702_msgbody) - 1)            

          break;    

   }    

  node702_msgbody[iCur] = 0;   

   return 0;

}

///////////////////////////

说是第一种,指针必须是new的才可以作为返回值,不让函数内的数组头,作为指针返回,会出现问题,

我操作之所以没有出现问题是因为,中没有别的代码来破坏之前的栈空间

可以返回CString的类型,CString可以保护起来字符串。

最好还是第二种方式

///////////////////////////

读取字段要加入iCur来分段读取,这样会好些

 

posted @ 2014-01-21 16:08  Peaceful-蓝蓝的  阅读(1426)  评论(0编辑  收藏  举报