zoukankan      html  css  js  c++  java
  • 返回值--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 ");   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来分段读取,这样会好些

  • 相关阅读:
    JS中的call()和apply()方法和bind()
    reactjs入门到实战(十)----one-first_app
    49-Reverse Linked List II
    48-Merge Sorted Array
    47-Generate Parentheses
    46.Valid Parentheses
    45-Letter Combinations of a Phone Number
    44-Count and Say
    43-Reverse Nodes in k-Group
    42-Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/orchid-sky/p/3528524.html
Copyright © 2011-2022 走看看