Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ memcpy CString到char*_C++_Memcpy - Fatal编程技术网

C++ memcpy CString到char*

C++ memcpy CString到char*,c++,memcpy,C++,Memcpy,我正在尝试使用memcpy()将CString复制到char*中,但执行起来有困难。实际上,只复制第一个字符。这是我的密码: CString str = _T("something"); char* buff = new char(); memcpy(buff, str, str.GetLength() + 1); 在此之后,buff包含的所有内容都是字母s您只分配了内存来保存char变量。要执行您想要的操作,您需要分配足够的内存来保存完整的字符串 CString str = _T("so

我正在尝试使用
memcpy()
CString
复制到
char*
中,但执行起来有困难。实际上,只复制第一个字符。这是我的密码:

CString str = _T("something");
char* buff  = new char();

memcpy(buff, str, str.GetLength() + 1);

在此之后,
buff
包含的所有内容都是字母
s

您只分配了内存来保存
char
变量。要执行您想要的操作,您需要分配足够的内存来保存完整的字符串

CString str = _T("something");
LPTSTR buff = new TCHAR[(str.GetLength()+1) * sizeof(TCHAR)]; //allocate sufficient memory
memcpy(buff, str, str.GetLength() + 1);

您只分配了内存来保存
char
变量。要执行您想要的操作,您需要分配足够的内存来保存完整的字符串

CString str = _T("something");
LPTSTR buff = new TCHAR[(str.GetLength()+1) * sizeof(TCHAR)]; //allocate sufficient memory
memcpy(buff, str, str.GetLength() + 1);
你是

  • 仅分配一个
    char
    ,这是不够的,除非
    CString
    为空,并且
  • 复制
    CString
    实例,而不是它所表示的字符串
  • 试一试

    你是

  • 仅分配一个
    char
    ,这是不够的,除非
    CString
    为空,并且
  • 复制
    CString
    实例,而不是它所表示的字符串
  • 试一试


    您可能混合了ASCII和Unicode字符串。如果使用Unicode设置编译,则
    CString
    存储Unicode字符串(每个字符两个字节,在本例中,每秒钟的字节为0,因此看起来像ASCII字符串终止符)

    如果需要所有ASCII码:

    CStringA str = "something";
    char* buff = new char[str.GetLength()+1];
    memcpy(buff, (LPCSTR)str, str.GetLength() + 1);
    
    如果需要所有Unicode:

    CStringW str = L"something";
    wchar_t* buff = new wchar_t[str.GetLength()+1];
    memcpy(buff, (LPCWSTR)str, sizeof(wchar_t)*(str.GetLength() + 1));
    
    如果要在两种设置上都工作:

    CString str = _T("something");
    TCHAR* buff = new TCHAR[str.GetLength()+1];
    memcpy(buff, (LPCTSTR)str, sizeof(TCHAR) * (str.GetLength() + 1));
    
    如果要将Unicode字符串转换为ASCII字符串:

    CString str = _T("something");
    char* buff = new char[str.GetLength()+1];
    memcpy(buff, (LPCSTR)CT2A(str), str.GetLength() + 1);
    
    还请识别从str到
    LPCSTR
    LPCWSTR
    LPCTSTR
    的强制转换以及正确的缓冲区分配(需要多个字符,而不仅仅是一个字符)


    另外,我不太确定这是否真的是你需要的。例如,一个
    strdup
    看起来比一个
    new
    +
    memcpy

    简单得多,您可能混合了ASCII和Unicode字符串。如果使用Unicode设置编译,则
    CString
    存储Unicode字符串(每个字符两个字节,在本例中,每秒钟的字节为0,因此看起来像ASCII字符串终止符)

    如果需要所有ASCII码:

    CStringA str = "something";
    char* buff = new char[str.GetLength()+1];
    memcpy(buff, (LPCSTR)str, str.GetLength() + 1);
    
    如果需要所有Unicode:

    CStringW str = L"something";
    wchar_t* buff = new wchar_t[str.GetLength()+1];
    memcpy(buff, (LPCWSTR)str, sizeof(wchar_t)*(str.GetLength() + 1));
    
    如果要在两种设置上都工作:

    CString str = _T("something");
    TCHAR* buff = new TCHAR[str.GetLength()+1];
    memcpy(buff, (LPCTSTR)str, sizeof(TCHAR) * (str.GetLength() + 1));
    
    如果要将Unicode字符串转换为ASCII字符串:

    CString str = _T("something");
    char* buff = new char[str.GetLength()+1];
    memcpy(buff, (LPCSTR)CT2A(str), str.GetLength() + 1);
    
    还请识别从str到
    LPCSTR
    LPCWSTR
    LPCTSTR
    的强制转换以及正确的缓冲区分配(需要多个字符,而不仅仅是一个字符)


    另外,我不太确定这是否真的是你需要的。例如,
    strdup
    看起来比
    new
    +
    memcpy

    strcpy
    吗?您只为1
    char
    分配了空间。然后尝试复制
    CString
    对象,而不是其包含的缓冲区。请注意,
    \u T()
    可以根据
    wchar\u T
    而不是
    char
    创建UNICODE字符串,具体取决于某些预处理器变量
    memcpy()
    忽略了这一点,并且总是复制字节。是否要
    strcpy
    ?您只为1
    char
    分配空间。然后尝试复制
    CString
    对象,而不是其包含的缓冲区。请注意,
    \u T()
    可以根据
    wchar\u T
    而不是
    char
    创建UNICODE字符串,具体取决于某些预处理器变量
    memcpy()
    忽略了这一点,总是复制字节。我建议
    LPTSTR buff=new TCHAR[str.GetLength()+1]
    来缓解我在问题下提到的问题。不,编辑错误,它应该是
    LPTSTR
    ,而不是
    LPSTR
    ,并且还应该乘以
    str.GetLength()+1
    通过
    sizeof(TCHAR)
    调用
    memcpy()
    ,正如沃纳在他的回答中所做的那样。我建议
    LPTSTR buff=new TCHAR[str.GetLength()+1]
    来缓解我在问题下提到的问题。不,编辑错误,它应该是
    LPTSTR
    ,而不是
    LPSTR
    ,在调用
    memcpy()
    时,您还应该将
    str.GetLength()+1
    乘以
    sizeof(TCHAR)
    ,正如沃纳在回答中所做的那样。