字符串是全汉字的,要将它赋值给一个char型数组,总是得不到正确的值,我知道这是因为汉字占用两个字节,可是这个问题要怎么处理呢?因为这个char是结构体的一个成员,还要将这个结构体写入CArray,所以要数组的每个元素都正确
CSting substr;
char un[32];strcpy(monitor.un,substr);
strncpy(monitor.un,substr,substr.GetLength());
memcpy(monitor.un,substr,substr.GetLength());这三种方法都试过了,后两种方法当改变substr的值赋给别的char的时候,un会在后面接上新的值。。
看了很多帖子说用unicode,但我刚开始学,不知道怎么用,如果哪位大虾能给予详细指导,感激不尽~~

解决方案 »

  1.   

    用unicode 版本的相应函数哦 wcscpy(。。)等
      

  2.   

    monitor.un// 这是怎么控制的?
      

  3.   

    un[substr.GetLength()] = 0;
    最后要有一个\0表示字符串结束。
      

  4.   

    不用UNICODE, 因为GB码虽然是多字节混编(1byte,2byte,4byet), 但也是以0结尾的.
      

  5.   

    回复各位:
    按照akirya和cnzdgs方法进行了尝试,字符串可以正确结尾,感谢。monitor是一个结构体,un是其中一个成员struct monitor_list_elem monitor;
    CArray<monitor_list_elem, monitor_list_elem&> monitor_list;得不到正确的值表现如下,这是运行相应语句后watch窗口的,- un 0x0011dbac "换热器阴极保护"
    [0] -69 '?
    [1] -69 '?
    [2] -56 '?
    [3] -56 '?
    [4] -58 '?
    [5] -9 '?
    [6] -46 '?
    [7] -11 '?
    [8] -68 '?运行写入CArray语句,monitor_list.SetAtGrow(k,monitor);
    watch窗口,monitor_list.un如下:
    - un 0x0000:0x0000 ""
    [0] CXX0030: Error: expression cannot be evaluated
    [1] CXX0030: Error: expression cannot be evaluated
    [2] CXX0030: Error: expression cannot be evaluated
    [3] CXX0030: Error: expression cannot be evaluated
    [4] CXX0030: Error: expression cannot be evaluated
    [5] CXX0030: Error: expression cannot be evaluated
    [6] CXX0030: Error: expression cannot be evaluated
    [7] CXX0030: Error: expression cannot be evaluated
    [8] CXX0030: Error: expression cannot be evaluated
    还望大家多多帮忙~
      

  6.   

    struct monitor_list_elem monitor; 
    CArray <monitor_list_elem, monitor_list_elem&> monitor_list; 
    下面的代码呢?
    怎么拷贝的贴出来。
      

  7.   

    memcpy(monitor.un,substr.GetBuffer(),substr.GetLength());再试试看行不行
      

  8.   

    结构体写入CArray代码:
    monitor_list.SetAtGrow(k,monitor); 
      

  9.   

    error C2660: 'GetBuffer' : function does not take 0 parameters
      

  10.   

    memcpy(monitor.un,substr.GetBuffer(substr.GetLength()),substr.GetLength());
      

  11.   

    GetBuffer填上了参数也不行。。