🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 代码: ``` import console console.open() // io.open(path,'rb') ,"rb" 表示以二进制读模式打开文件 var datFile = io.open('wltc28.dat','rb') //IDBLOCK模块结构体,共64字节 var IDBlockStruct = { byte IDB_fileIdentifier[8]; byte IDB_formatIdentifier[8]; byte IDB_programIdentifier[8]; word IDB_defaultByteOrder; // 大小端 0 - little endian ; 1-big endian word IDB_defaultFloatingPointFormat; // 浮点格式 0 - IEEE 754标准 word IDB_VersionNumber; word IDB_codePagenumber; byte IDB_reserved_1[2]; byte IDB_reserved_2[26]; word IDB_standardFlags; word IDB_customFlags; } // 模块结构体的大小(64字节数) console.log(raw.sizeof(IDBlockStruct)) // 移动读写指针到文件开始 datFile.seek() //再读取结构体 var idBlockstruct= datFile.read(IDBlockStruct) // 输出表对象 console.dump(idBlockstruct) /* console.dumpTable(idBlockstruct) for(k,v in idBlockstruct){ console.log( k,v) } */ console.pause(true) ``` # 结果: ![](https://img.kancloud.cn/1c/a3/1ca31692d48c73db1a6c708aa231b909_598x287.png) # console.dumpTable()输出的idBlockstruct结构体对象(有整理便于好看): ``` { IDB_vers="3.00 "; IDB_byte_order=0; IDB_ver=300; IDB_prog="TGT 15.0"; IDB_res2='\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'; IDB_sf=0; IDB_cf=0; IDB_res1='\0\0'; IDB_mdf="MDF "; IDB_float_format=0; _struct="byte IDB_mdf[8]; byte IDB_vers[8]; byte IDB_prog[8]; word IDB_byte_order; word IDB_float_format; word IDB_ver; word IDB_cpage; byte IDB_res1[2]; byte IDB_res2[26]; word IDB_sf; word IDB_cf"; IDB_cpage=0 } ``` # 实际应用 * 实际在应用中并不象上面这样,ID模块是个规整的结构,如里遇到不规整的(即字数量不相符),就不能这样用了 * 推荐是一个一个的取值,如ID模块中有11个字段,就用11个结构体去取值,以免错位。