C序列化或反序列化库tpl

转载于:http://www.oschina.net/p/tpl?fromerr=PfwYBBBS

http://coolshell.cn/articles/878.html

http://troydhanson.github.io/tpl/userguide.html#_on_windows

代码示例:
把一个数组(“序号”和“人名”)序例化到文件中。

#include "tpl.h"

int main(int argc, char *argv[]) {
    tpl_node *tn;
    int id=0;
    char *name, *names[] = { "joe", "bob", "cary" };

    tn = tpl_map("A(is)", &id, &name);

    for(name=names[0]; id < 3; name=names[++id]) {
        tpl_pack(tn,1);
    }

    tpl_dump(tn, TPL_FILE, "users.tpl");
    tpl_free(tn);
}

把上面那个序列化到文件的“序号”和“人名”反序列化回来。

#include "tpl.h"

int main(int argc, char *argv[]) {
    tpl_node *tn;
    int id;
    char *name;

    tn = tpl_map("A(is)", &id, &name);
    tpl_load(tn, TPL_FILE, "users.tpl");

    while ( tpl_unpack(tn,1) > 0 ) {
        printf("id %d, user %s\n", id, name);
        free(name);
    }
    tpl_free(tn);
}

你可能感兴趣的:(C-C++基础)