请看下面两段代码
CString a = "abc";
a += '/0';
CString b = a;
CString a = "abc";
CString b = a + '\0';请问这两段代码有区别吗?为什么我用的时候(以b为一个函数的参数),第一段代码正确运行,第二段代码就总是运行时报错说无法读源文件,在网上查的时候有人说看一下CString是如何重载+就知道原因了,但我没找着那段代码,请大家帮忙,谢谢。

解决方案 »

  1.   

    那后面那段代码这样写呢
    CString a = "abc";
    CString b = a ;
    b += '/0';
      

  2.   

    不要尝试在字符串中插入、添加'\0',否则其行为完全依赖于实现。你可以找到这个CString具体现象的原因,但是这样的“原因”对你毫无意义,因为你首先要避免的是给字符串加上'\0'!
      

  3.   

    我估计是operater+和CString::CString(LPCTSTR )中某一个函数(更可能是后者)把多余的\0忽略了,如果你遇到的问题和字符串后面是不是有两个\0有关的话
      

  4.   

    这样吧,把我的题目大意改一下,在哪里能找到CString的源代码?
      

  5.   

    你Debug下调试一下看看有什么不同不就很清楚了
      

  6.   

    是不是CString 定义变量的时候不支持 + 运算啊?
      

  7.   

    CString::operator + 
    friend CString operator +( const CString& string1, const CString& string2 );
    throw( CMemoryException );friend CString operator +( const CString& string, TCHAR ch );
    throw( CMemoryException );friend CString operator +( TCHAR ch, const CString& string );
    throw( CMemoryException );friend CString operator +( const CString& string, LPCTSTR lpsz );
    throw( CMemoryException );friend CString operator +( LPCTSTR lpsz, const CString& string );
    throw( CMemoryException );Return ValueA CString object that is the temporary result of the concatenation. This return value makes it possible to combine several concatenations in the same expression.Parametersstring, string1, string2CString objects to concatenate.chA character to concatenate to a string or to concatenate a string to.lpszA pointer to a null-terminated character string.ResThe + concatenation operator joins two strings and returns a CString object. One of the two argument strings must be a CString object. The other can be a character pointer or a character. You should be aware that memory exceptions may occur whenever you use the concatenation operator since new storage may be allocated to hold temporary data. ExampleThe following example demonstrates the use of CString::operator +.// example for CString::operator +
    CString s1( "abc" );
    CString s2( "def" );
    ASSERT( (s1 + s2 ) == "abcdef" );
    CString s3;
    s3 = CString( "abc" ) + "def" ; // Correct
    s3 = "abc" + "def"; 
    // Wrong! The first argument must be a CString.
      

  8.   

    但是CString不以'\0'结束啊,有些参数就要求以‘\0’结束,所以我才加的
      

  9.   

    据我所知,CString以\0结束
      

  10.   

    CString本身就是以\0结束的,不然是怎么知道CString是什么时候结束的呢。多看看基本知识。
      

  11.   

    那这样吧,有时间的同仁可以试下用SHFileOperate函数写一个函数实现下删除,其中的参数就用CString,看看不加'/0'能不能实现
    int deleteFile(CString fileName){
    ...
    }
      

  12.   

    楼主知道为什么SHFileOperate有问题么?不要知其然不知起所以然!SHFileOperation需要得是“两个”\0,而一般CString只有一个\0,当然不行!
      

  13.   

    SHFileOperation需要得是“两个”\0,所以我才要手动的加\0啊,但是
    CString a = "abc";
    a += '/0';
    CString b = a;
    CString a = "abc";
    CString b = a + '\0'
    这两段代码都是加\0,效果是不一样的,第一段正确,第二段错误哦,我想知道为什么
      

  14.   

    不知道你验证“正确性”的方法是什么,'/0'从来就不是合法的字符,不知道你为什么认为它正确的。
    虽然我们可以在string后面+'\0',但是这个字符串不能再进行任何其他字符串运算。任何运算都可能造成后续的哪个'\0'丢失!所以如果你想给SHFileOperation准备输入参数,建议你开辟一个TCHAR缓冲区,自己通过_tcscpy逐个拷贝,而不要指望用std::string或者CString通过+'\0'的方法加
      

  15.   

    验证正确性地方法就是我那个deleteFile的函数可以实现
      

  16.   

    要查看CString 的源代码 可以在程序中断点进入查看啊
      

  17.   

    CString是一个类,要像对待一个类那样对待它。"CString 是以***结尾"这样的讨论就跑偏了
      

  18.   

    问题还是没有得到解决,
    CString a = "abc";
    a += '/0';
    CString b = a;
    CString a = "abc";
    CString b = a + '\0';
    到底有什么区别呢?