void WriteStringsToText(CSting S[],int length)
{
    CFile file;
   file.Open("MyFile",CFile::modeCreate);
  
   CString str;
str.Empty();
   for(int i=0;i<length;i++)
   {
     str=str+S[i];
   }
   file.Write(str,str.GetLength());
   file.Close();
}

解决方案 »

  1.   

    public Exception TextWrite(string strContain,string strFilePath,bool blOverWrite)
    {
      Exception rtnExcept=new Exception("成功保存");
      try
      {
        byte[] byteContain=StringToBytes(strContain);
        FileStream fs;
        if(blOverWrite)
          fs=new FileStream(strFilePath,FileMode.Create);
        else
          fs=new FileStream(strFilePath,FileMode.Append,FileAccess.Write);
        fs.Write(byteContain,0,byteContain.length);
      }
      catch(Exception e)
      {
        rtnExcept=e;
      }
      final
      {
        if(fs!=null)
          fs.Close();
      }
      return rtnExcept;
    }
    public byte[] StringToBytes(string strSrc)
    {
      try
      {
        int byteLen=strSrc.length;
        for(int i=0;i<strSrc.length;i++)
          if(strSrc[i]>255)
            byteLen++;
        byte[] rtnBytes=new byte[byteLen];
        for(int i=0,j=0;i<strSrc.length;i++)
          if(strSrc[i]>255)
          {
            rtnBytes[j++]=Convert.ToByte(strSrc[i]>>8);
            rtnBytes[j++]=Convert.ToByte(strSrc[i]&255);
          }
          else
          {
            rtnBytes[j++]=Convert.ToByte(strSrc[i]);
          }
      }
      catch(Exception e)
      {
        throw(e);
      }
      return rtnBytes;
    }
      

  2.   

    triout兄,返回值是一个Exception类型的?!比较怪异啊!
      

  3.   

    看了triout的代码我想问大家一个问题:在代码中捕捉了异常,而对异常的处理仅仅是把这个异常再次抛出,那还有没有必要去捕捉这个异常?我在写代码的时候也常常在想这个问题,我捕捉了异常,但又不知道怎么去处理这个异常,只好仍将他再次抛出。
      

  4.   

    我的权限就是指是否有权限读写,类似C中的r,r+,rw,之类的。
    是不是只在*nix 系统中这个概念才有用呢?
      

  5.   

    之所以返回一个异常,是因为问题要求有错误返回,这些代码只有异常时会报告错误,其他的则肯定成功保存了。
    另外,返回异常并不是抛出异常,抛出是使用THROW语句实现,在该函数的调用过程中还是会被捕捉到,而我的返回异常是不会被TRY检测到。
    处理异常很简单,最简单的使用方法就是异常转换成STRING,就知道其内容了,另外可以查看相关资料,而且异常有很多继承的类型。