在具有TPL的结构中序列化wchar_t*

在具有TPL的结构中序列化wchar_t*,c,serialization,unicode,task-parallel-library,C,Serialization,Unicode,Task Parallel Library,我正在尝试使用tpl序列化包含wchar\u t*字符串的结构 我的代码如下所示,但不起作用: #include <stdio.h> #include <locale.h> #include <string.h> #include <wchar.h> #include "tpl.h" struct chinese_t { wchar_t *chars; }; int main() { tpl_node *tn; struct c

我正在尝试使用tpl序列化包含wchar\u t*字符串的结构

我的代码如下所示,但不起作用:

#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <wchar.h>
#include "tpl.h"

struct chinese_t {
    wchar_t *chars;
};


int main() {

tpl_node *tn;


struct chinese_t cstr;
cstr.chars = L"字符串";

tn = tpl_map("S(s)", &cstr);
tpl_pack( tn, 0 );
tpl_dump(tn, TPL_FILE, "string.tpl");
tpl_free(tn);


struct chinese_t cstr2;

tn = tpl_map( "S(s)", &cstr2);
//tpl_load(tn, TPL_MEM, buffer, len);
tpl_load(tn, TPL_FILE, "string.tpl");
tpl_unpack(tn, 0);
tpl_free(tn);


printf("%ls\n", cstr2.chars);
return;
}
#包括
#包括
#包括
#包括
#包括“tpl.h”
结构中文{
wchar_t*chars;
};
int main(){
tpl_节点*tn;
结构中国科学技术委员会;
cstr.chars=L“字符串";
tn=tpl_图(“S”和cstr);
第三方物流包装(tn,0);
tpl_转储(tn,tpl_文件,“string.tpl”);
无第三方物流(tn);
结构中文(cstr2),;
tn=tpl_图(“S”和cstr2);
//tpl_负载(总氮、tpl_膜、缓冲器、透镜);
tpl_加载(tn,tpl_文件,“string.tpl”);
tpl_开箱(tn,0);
无第三方物流(tn);
printf(“%ls\n”,cstr2.chars);
返回;
}

如果我替换中国人“字符串带有“1234”的字符串,它只打印“1”——如果我更改定义,使结构使用一个char*(并且我只在其中插入ASCII字符)它工作得很好。但是我不知道如何让它正确地序列化和反序列化wchar_t*字符串。

我以前没有使用过tpl,但是快速查看文档,它似乎不直接支持宽字符。您看到的字符串“1234”的行为与包含字节“1\x002\x003\x004\x00\x00\x00\x00”的UTF-16编码字符串一致,tpl将其视为仅以NUL结尾的字节字符串“1\x00”

看起来您最好的选择可能是:

  • 将tpl中的宽字符串表示为16位整数数组
  • 将字符串编码为UTF-8
    char
    strings并使用tpl字符串类型;或
  • 修改tpl以包含宽字符串类型

啊,我错过了文档中关于如何指定要添加到结构中的哪些无界数组的部分。谢谢你的回答!