读文件到CString中去        CString asmStr;
FILE* pFile=fopen(filename,"r+b");
long fsize = _filelength(fileno(pFile));
fseek(pFile,0,SEEK_SET);
fread(asmStr.GetBuffer(fsize),fsize,1,pFile);
asmStr.ReleaseBuffer();最后出了一个断言错误  nlength<=getdata()->nalloclength清大家帮忙看看!谢谢!

解决方案 »

  1.   

    是在asmStr.ReleaseBuffer(); 这行报的错吗?
      

  2.   

    CString asmStr('\0',100); 

    FILE* pFile=fopen(_T("d:\\aa.txt"),"r+b"); 
    long fsize = _filelength(fileno(pFile)); 
    fseek(pFile,0,SEEK_SET); 
    fread(asmStr.GetBuffer(fsize),fsize,1,pFile); 
    asmStr.ReleaseBuffer(); 
    fclose(pFile);
      

  3.   

    楼上的方法么用啊,是在asmStr.ReleaseBuffer();出错跟进去发现ReleaseBuffer 中 CString重新计算缓冲区的大小,比我规定的fsize大13个字节
      

  4.   

    大家帮忙看看啊,到底是怎么回事啊,怎么
    void ReleaseBuffer( int nNewLength = -1 )
    {
    if( nNewLength == -1 )
    {
    nNewLength = StringLength( m_pszData );
    }
    SetLength( nNewLength );
    }nNewLength 算出来,比fsize大13个字节呢?
      

  5.   

    "r+b"方式读,中间包含非文本信息,致使你的缓冲内部是NULL结尾的字符串,而调用时又是按照默认的参数-1执行的。建议你读取非文本信息时不要用CString。void ReleaseBuffer(
       int nNewLength = -1
    );
    Parameter
    nNewLength 
    The new length of the string in characters, not counting a null terminator. If the string is null terminated, the -1 default value sets the CSimpleStringT size to the current length of the string. 如果只是字符串,可以如下做LPTSTR p = s.GetBuffer( XXX );
    s.ReleaseBuffer( );  // Surplus memory released, p is now invalid.
      

  6.   

    如果是文本文件,分配的空间要足够长,最好不要用CString
      

  7.   

    谢谢WizardK,我还是有点问题,你能再帮我看看么?
    你说的非文本信息,是不是就是指的'\0'这样的信息?
    那long fsize = _filelength(fileno(pFile)); 我这个fsize算进去'\0'这个的大小了么?就算没算进去的话fread(asmStr.GetBuffer(fsize),fsize,1,pFile); 我已经指定大小了,为什么最后ReleaseBuffer里算出的会大一点呢?
      

  8.   

    是fread(asmStr.GetBuffer(fsize),fsize,1,pFile); 这一行出错了,msdn指出的fread函数的第一个参数是void*类型的,当定义char*型指针,然后new内存空间的方法调用fread函数就会发生错误。
    自己网上查下相关文章吧。
      

  9.   

    最后我还是用的这种笨办法做的!
    char* temp;
    temp=(char*)malloc(fsize);
    fread(temp,fsize,1,pFile);
    //asmStr.ReleaseBuffer();
    temp[fsize-1]='\0';
    asmStr=temp;
    free(temp);
    但是我很想知道为why!
    望高人指点!
      

  10.   

    3000多个字节我觉得很奇怪啊,我只读进去fsize个字节的东西啊!fsize后面的东西是哪来的,况且getbuffer只给fsize的大小,怎么又变大了?
      

  11.   

    如果你用CString,那么你申请的空间为fsize,当文件最后一个字节不等于‘\0’时,那么CString就没有字符串结束符号了。
    CString asmStr; 
    char *ptr = asmStr.GetBuffer(fsize+1);
    memset(ptr,0,fsize+1);
    fread(ptr,fsize,1,pFile);
    asmStr.ReleaseBuffer();
      

  12.   

    恩,谢谢各位大大了,有点感觉了,占时用这个方法吧,可能是releasebuffer里计算长度是需要‘\0’。
    char* temp=asmStr.GetBuffer(fsize);

    fread(temp,fsize,1,pFile);
    temp[fsize-1]='\0';
    asmStr.ReleaseBuffer();
      

  13.   


    非文本信息是指组成不是都有可见字符组成的。
    fsize是找到文件的EOF为结尾,中间当然会包含0X00。
    建议你这种缓冲分配还是不要用CSTRING了,效率低。
      

  14.   

    CFile f;
    f.Open(fileName,CFile::modeRead);
    int n = f.GetLength();
    char* temp = new char[n+1];
    f.Read(temp,n);
    temp[n] = 0;
    asmStr = temp;
    delete [] temp;反正我是从来不往CString里面直接灌数据,如果需要用GetBuffer也不要给他指定长度,这样可以获得整个长度,release的时候就不出问题了。
    你上面的temp[fsize-1]='\0'; 吃掉以一个字符。