本文共 2912 字,大约阅读时间需要 9 分钟。
XDR-初探XDR对变长类型空间的管理。--log
2014年4月8日22:35:00
.x文件
enum colors{ RED = 2, YELLOW = 3, BLUE = 5, GREEN = 0x10};struct lww_int{ int m_nAge; bool m_bMale; string m_sName<255>; colors m_ayColor[2];};union lww_union switch(colors clr){case RED: int m_nAge;case YELLOW: bool m_bMale;case BLUE: string m_sName<255>;case GREEN: colors m_ayColor[2];};C程序:
#include#include #include "lww.h"//int main()//{// printf("begin\n");//// FILE * pFile = fopen("lww.txt","wb");// XDR XDR;// xdrstdio_create(&XDR,pFile,XDR_ENCODE);//// colors aRED = RED;// colors aYELLOW = YELLOW;// colors aBLUE = BLUE;// colors aGREEN = GREEN;// lww_int aLwwInt;// aLwwInt.m_nAge = 28;// aLwwInt.m_bMale = TRUE;// aLwwInt.m_sName = (char *)"1234";// aLwwInt.m_ayColor[0] = GREEN;// aLwwInt.m_ayColor[1] = BLUE;// lww_union aLwwUnion;// aLwwUnion.clr = RED;// aLwwUnion.lww_union_u.m_nAge = 28 * 2;//// xdr_colors(&XDR,&aRED);// xdr_colors(&XDR,&aYELLOW);// xdr_colors(&XDR,&aBLUE);// xdr_colors(&XDR,&aGREEN);// xdr_lww_int(&XDR,&aLwwInt);// xdr_lww_union(&XDR,&aLwwUnion);//// xdr_destroy(&XDR);// fclose(pFile);//// printf("end\n");//// return 0;//}void printColor(colors aColor){ switch(aColor) { case RED: printf("红色\n"); break; case YELLOW: printf("黄色\n"); break; case BLUE: printf("蓝色\n"); break; case GREEN: printf("绿色\n"); break; default: printf("错误色\n"); break; } return;}void printBool(bool_t bPrint){ switch(bPrint) { case TRUE: printf("真\n"); break; case FALSE: printf("假\n"); break; default: printf("错误\n"); break; } return;}lww_int aLwwInt;void writeTLogFile(char * str){ FILE * pFileW = fopen("log.txt","a+"); if(NULL == pFileW) { return; } fprintf(pFileW,"\n%s",str); fclose(pFileW); return;}void writeBLogFile(char * str,int nCount){ FILE * pFileW = fopen("log.txt","ab+"); if(NULL == pFileW) { return; } fwrite(str,1,nCount,pFileW); fclose(pFileW); return;}int main(){ printf("begin\n"); writeTLogFile("加载前:"); writeBLogFile((char *)&aLwwInt,sizeof(aLwwInt)); FILE * pFile = fopen("lww.txt","rb"); XDR XDR; xdrstdio_create(&XDR,pFile,XDR_DECODE); colors aColor; char sName[255] = {0}; //lww_int aLwwInt; //aLwwInt.m_sName = sName; xdr_colors(&XDR,&aColor); printColor(aColor); xdr_colors(&XDR,&aColor); printColor(aColor); xdr_colors(&XDR,&aColor); printColor(aColor); xdr_colors(&XDR,&aColor); printColor(aColor); xdr_lww_int(&XDR,&aLwwInt); printf("%d\n",aLwwInt.m_nAge); printBool(aLwwInt.m_bMale); printf("%s\n",aLwwInt.m_sName); printColor(aLwwInt.m_ayColor[0]); printColor(aLwwInt.m_ayColor[1]); writeTLogFile("加载后:"); writeBLogFile((char *)&aLwwInt,sizeof(aLwwInt)); lww_union aLwwUnion; xdr_lww_union(&XDR,&aLwwUnion); if(RED == aLwwUnion.clr) { printf("%d\n",aLwwUnion.lww_union_u.m_nAge); } xdr_free((xdrproc_t)xdr_lww_int,(char *)&aLwwInt); writeTLogFile("释放后:"); writeBLogFile((char *)&aLwwInt,sizeof(aLwwInt)); xdr_destroy(&XDR); fclose(pFile); printf("end\n"); return 0;}
转载地址:http://knwci.baihongyu.com/