• C++ 实现读文件之 字节方式的


    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. #include
    4. #include
    5. #define FILENAME "DataOutput_XWR14xx.bin"
    6. #define RECORDSTXT "records.txt"
    7. void appendTxtLine(char* str) {
    8. FILE* file;
    9. file = fopen("records.txt", "a"); // 使用追加模式打开文件
    10. if (file == NULL) {
    11. perror("无法打开文件");
    12. return;
    13. }
    14. fprintf(file, "%s\n", str);
    15. // 关闭文件
    16. fclose(file);
    17. }
    18. typedef struct Header {
    19. uint16_t magicWord[4];
    20. uint32_t version;
    21. uint32_t totalPacketLen;
    22. uint32_t platform;
    23. uint32_t frameNumber;
    24. uint32_t timeCpuCycles;
    25. uint32_t numDetectedObj;
    26. uint32_t numTLVs;
    27. uint32_t subFrameNumber;
    28. }Header_t;
    29. Header_t head;
    30. typedef struct MmwDemo_output_message_tl_t
    31. {
    32. //tlv是通讯协议,在bin里这个type一个是0一个是1,
    33. //用来识别他之后的数据是output还是rangeprofile,
    34. uint32_t type; //0 //1
    35. //length告知有多少字节数据
    36. uint32_t length;
    37. } MmwDemo_output_message_tl;
    38. MmwDemo_output_message_tl TLV_head, TLV_head1;
    39. typedef struct VitalSignsDemo_OutputStats_t
    40. {
    41. uint16_t rangeBinIndexMax; // 1
    42. uint16_t rangeBinIndexPhase; // 1
    43. float maxVal; // 2
    44. uint32_t processingCyclesOut; // 3
    45. uint16_t rangeBinStartIndex; // 4
    46. uint16_t rangeBinEndIndex; // 4
    47. float unwrapPhasePeak_mm; // 5
    48. float outputFilterBreathOut; // 6
    49. float outputFilterHeartOut; // 7
    50. float heartRateEst_FFT; // 8心率FFT
    51. float heartRateEst_FFT_4Hz; // 9
    52. float heartRateEst_xCorr; // 10
    53. float heartRateEst_peakCount_filtered; // 11峰值间隔
    54. float breathingRateEst_FFT; // 12呼吸FFT
    55. float breathingRateEst_xCorr; // 13
    56. float breathingRateEst_peakCount; // 14
    57. float confidenceMetricBreathOut; // 15呼吸置信度
    58. float confidenceMetricBreathOut_xCorr; // 16
    59. float confidenceMetricHeartOut; // 17心率置信度
    60. float confidenceMetricHeartOut_4Hz; // 18
    61. float confidenceMetricHeartOut_xCorr; // 19
    62. float sumEnergyBreathWfm; // 20
    63. float sumEnergyHeartWfm; // 21
    64. float motionDetectedFlag; // 22
    65. float breathingRateEst_harmonicEnergy; // 23
    66. float heartRateEst_harmonicEnergy; // 24
    67. float reserved7; //25
    68. float reserved8; //26
    69. float reserved9; //27
    70. float reserved10; //28
    71. float reserved11; //29
    72. float reserved12; //30
    73. float reserved13; //31
    74. float reserved14; //32
    75. } VitalSignsDemo_OutputStats;
    76. VitalSignsDemo_OutputStats outputStats;
    77. void printOutputStats() {
    78. char buf[1024];
    79. printf("\nVitalSignsDemo_OutputStats:\n");
    80. printf("rangeBinIndexMax = %hd\n", outputStats.rangeBinIndexMax); //1
    81. printf("rangeBinIndexPhase = %hd\n", outputStats.rangeBinIndexPhase); //1
    82. printf("maxVal = %.2f\n", outputStats.maxVal); //2
    83. printf("processingCyclesOut = %d\n", outputStats.processingCyclesOut); //3
    84. printf("rangeBinStartIndex = %hd\n", outputStats.rangeBinStartIndex); //4
    85. printf("rangeBinEndIndex = %hd\n", outputStats.rangeBinEndIndex); //4
    86. printf("unwrapPhasePeak_mm = %.2f\n", outputStats.unwrapPhasePeak_mm); //5
    87. printf("outputFilterBreathOut = %.2f\n", outputStats.outputFilterBreathOut); //6
    88. printf("outputFilterHeartOut = %.2f\n", outputStats.outputFilterHeartOut); //7
    89. printf("heartRateEst_FFT = %.2f\n", outputStats.heartRateEst_FFT); //8
    90. printf("heartRateEst_FFT_4Hz = %.2f\n", outputStats.heartRateEst_FFT_4Hz); //9
    91. printf("heartRateEst_xCorr = %.2f\n", outputStats.heartRateEst_xCorr); //10
    92. printf("heartRateEst_peakCount_filtered = %.2f\n", outputStats.heartRateEst_peakCount_filtered); //11
    93. printf("breathingRateEst_FFT = %.2f\n", outputStats.breathingRateEst_FFT); //12
    94. printf("breathingRateEst_xCorr = %.2f\n", outputStats.breathingRateEst_xCorr); //13
    95. printf("breathingRateEst_peakCount = %.2f\n", outputStats.breathingRateEst_peakCount); //14
    96. printf("confidenceMetricBreathOut = %.2f\n", outputStats.confidenceMetricBreathOut); //15
    97. printf("confidenceMetricBreathOut_xCorr = %.2f\n", outputStats.confidenceMetricBreathOut_xCorr); //16
    98. printf("confidenceMetricHeartOut = %.2f\n", outputStats.confidenceMetricHeartOut); //17
    99. printf("confidenceMetricHeartOut_4Hz = %.2f\n", outputStats.confidenceMetricHeartOut_4Hz); //18
    100. printf("confidenceMetricHeartOut_xCorr = %.2f\n", outputStats.confidenceMetricHeartOut_xCorr); //19
    101. printf("sumEnergyBreathWfm = %.2f\n", outputStats.sumEnergyBreathWfm); //20
    102. printf("sumEnergyHeartWfm = %.2f\n", outputStats.sumEnergyHeartWfm); //21
    103. printf("motionDetectedFlag = %.2f\n", outputStats.motionDetectedFlag); //22
    104. printf("breathingRateEst_harmonicEnergy = %.2f\n", outputStats.breathingRateEst_harmonicEnergy); //23
    105. printf("heartRateEst_harmonicEnergy = %.2f\n", outputStats.heartRateEst_harmonicEnergy); //24
    106. printf("reserved7 = %.2f\n", outputStats.reserved7); //25
    107. printf("reserved8 = %.2f\n", outputStats.reserved8); //26
    108. printf("reserved9 = %.2f\n", outputStats.reserved9); //27
    109. printf("reserved10 = %.2f\n", outputStats.reserved10); //28
    110. printf("reserved11 = %.2f\n", outputStats.reserved11); //29
    111. printf("reserved12 = %.2f\n", outputStats.reserved12); //30
    112. printf("reserved13 = %.2f\n", outputStats.reserved13); //31
    113. printf("reserved14 = %.2f\n", outputStats.reserved14); //32
    114. sprintf(buf, "\nVitalSignsDemo_OutputStats:");
    115. appendTxtLine(buf);
    116. sprintf(buf, "rangeBinIndexMax = %hd", outputStats.rangeBinIndexMax); //1
    117. appendTxtLine(buf);
    118. sprintf(buf, "rangeBinIndexPhase = %hd", outputStats.rangeBinIndexPhase); //1
    119. appendTxtLine(buf);
    120. sprintf(buf, "maxVal = %.2f", outputStats.maxVal); //2
    121. appendTxtLine(buf);
    122. sprintf(buf, "processingCyclesOut = %d", outputStats.processingCyclesOut); //3
    123. appendTxtLine(buf);
    124. sprintf(buf, "rangeBinStartIndex = %hd", outputStats.rangeBinStartIndex); //4
    125. appendTxtLine(buf);
    126. sprintf(buf, "rangeBinEndIndex = %hd", outputStats.rangeBinEndIndex); //4
    127. appendTxtLine(buf);
    128. sprintf(buf, "unwrapPhasePeak_mm = %.2f", outputStats.unwrapPhasePeak_mm); //5
    129. appendTxtLine(buf);
    130. sprintf(buf, "outputFilterBreathOut = %.2f", outputStats.outputFilterBreathOut); //6
    131. appendTxtLine(buf);
    132. sprintf(buf, "outputFilterHeartOut = %.2f", outputStats.outputFilterHeartOut); //7
    133. appendTxtLine(buf);
    134. sprintf(buf, "heartRateEst_FFT = %.2f", outputStats.heartRateEst_FFT); //8
    135. appendTxtLine(buf);
    136. sprintf(buf, "heartRateEst_FFT_4Hz = %.2f", outputStats.heartRateEst_FFT_4Hz); //9
    137. appendTxtLine(buf);
    138. sprintf(buf, "heartRateEst_xCorr = %.2f", outputStats.heartRateEst_xCorr); //10
    139. appendTxtLine(buf);
    140. sprintf(buf, "heartRateEst_peakCount_filtered = %.2f", outputStats.heartRateEst_peakCount_filtered); //11
    141. appendTxtLine(buf);
    142. sprintf(buf, "breathingRateEst_FFT = %.2f", outputStats.breathingRateEst_FFT); //12
    143. appendTxtLine(buf);
    144. sprintf(buf, "breathingRateEst_xCorr = %.2f", outputStats.breathingRateEst_xCorr); //13
    145. appendTxtLine(buf);
    146. sprintf(buf, "breathingRateEst_peakCount = %.2f", outputStats.breathingRateEst_peakCount); //14
    147. appendTxtLine(buf);
    148. sprintf(buf, "confidenceMetricBreathOut = %.2f", outputStats.confidenceMetricBreathOut); //15
    149. appendTxtLine(buf);
    150. sprintf(buf, "confidenceMetricBreathOut_xCorr = %.2f", outputStats.confidenceMetricBreathOut_xCorr); //16
    151. appendTxtLine(buf);
    152. sprintf(buf, "confidenceMetricHeartOut = %.2f", outputStats.confidenceMetricHeartOut); //17
    153. appendTxtLine(buf);
    154. sprintf(buf, "confidenceMetricHeartOut_4Hz = %.2f", outputStats.confidenceMetricHeartOut_4Hz); //18
    155. appendTxtLine(buf);
    156. sprintf(buf, "confidenceMetricHeartOut_xCorr = %.2f", outputStats.confidenceMetricHeartOut_xCorr); //19
    157. appendTxtLine(buf);
    158. sprintf(buf, "sumEnergyBreathWfm = %.2f", outputStats.sumEnergyBreathWfm); //20
    159. appendTxtLine(buf);
    160. sprintf(buf, "sumEnergyHeartWfm = %.2f", outputStats.sumEnergyHeartWfm); //21
    161. appendTxtLine(buf);
    162. sprintf(buf, "motionDetectedFlag = %.2f", outputStats.motionDetectedFlag); //22
    163. appendTxtLine(buf);
    164. sprintf(buf, "breathingRateEst_harmonicEnergy = %.2f", outputStats.breathingRateEst_harmonicEnergy); //23
    165. appendTxtLine(buf);
    166. sprintf(buf, "heartRateEst_harmonicEnergy = %.2f", outputStats.heartRateEst_harmonicEnergy); //24
    167. appendTxtLine(buf);
    168. sprintf(buf, "reserved7 = %.2f", outputStats.reserved7); //25
    169. appendTxtLine(buf);
    170. sprintf(buf, "reserved8 = %.2f", outputStats.reserved8); //26
    171. appendTxtLine(buf);
    172. sprintf(buf, "reserved9 = %.2f", outputStats.reserved9); //27
    173. appendTxtLine(buf);
    174. sprintf(buf, "reserved10 = %.2f", outputStats.reserved10); //28
    175. appendTxtLine(buf);
    176. sprintf(buf, "reserved11 = %.2f", outputStats.reserved11); //29
    177. appendTxtLine(buf);
    178. sprintf(buf, "reserved12 = %.2f", outputStats.reserved12); //30
    179. appendTxtLine(buf);
    180. sprintf(buf, "reserved13 = %.2f", outputStats.reserved13); //31
    181. appendTxtLine(buf);
    182. sprintf(buf, "reserved14 = %.2f", outputStats.reserved14); //32
    183. appendTxtLine(buf);
    184. }
    185. void printTLV_head(MmwDemo_output_message_tl* TLV_head) {
    186. printf("\nTLV_head:\n");
    187. printf("TLV_head type = %d\n", TLV_head->type);
    188. printf("TLV_head length = %d\n", TLV_head->length);
    189. char buf[1024];
    190. appendTxtLine((char*)"\nTLV_head:");
    191. sprintf(buf, "TLV_head type = %d", TLV_head->type);
    192. appendTxtLine(buf);
    193. sprintf(buf, "TLV_head length = %d", TLV_head->length);
    194. appendTxtLine(buf);
    195. }
    196. void printHead() {
    197. char buf[1024];
    198. printf("Head:\n");
    199. printf("magicWord[0] = %#06x\t", head.magicWord[0]);
    200. printf("magicWord[1] = %#06x\t", head.magicWord[1]);
    201. printf("magicWord[2] = %#06x\t", head.magicWord[2]);
    202. printf("magicWord[3] = %#06x\n", head.magicWord[3]);
    203. printf("version = %#010x\n", head.version);
    204. printf("totalPacketLen = %d\n", head.totalPacketLen);
    205. printf("platform = %#010x\n", head.platform);
    206. printf("frameNumber = %d\n", head.frameNumber);
    207. printf("timeCpuCycles = %d\n", head.timeCpuCycles);
    208. printf("numDetectedObj = %d\n", head.numDetectedObj);
    209. printf("numTLVs = %d\n", head.numTLVs);
    210. printf("subFrameNumber = %d\n", head.subFrameNumber);
    211. sprintf(buf, "\nHead:");
    212. appendTxtLine(buf);
    213. sprintf(buf, "magicWord[0] = %#06x", head.magicWord[0]);
    214. appendTxtLine(buf);
    215. sprintf(buf, "magicWord[1] = %#06x", head.magicWord[1]);
    216. appendTxtLine(buf);
    217. sprintf(buf, "magicWord[2] = %#06x", head.magicWord[2]);
    218. appendTxtLine(buf);
    219. sprintf(buf, "magicWord[3] = %#06x", head.magicWord[3]);
    220. appendTxtLine(buf);
    221. sprintf(buf, "version = %#010x", head.version);
    222. appendTxtLine(buf);
    223. sprintf(buf, "totalPacketLen = %d", head.totalPacketLen);
    224. appendTxtLine(buf);
    225. sprintf(buf, "platform = %#010x", head.platform);
    226. appendTxtLine(buf);
    227. sprintf(buf, "frameNumber = %d", head.frameNumber);
    228. appendTxtLine(buf);
    229. sprintf(buf, "timeCpuCycles = %d", head.timeCpuCycles);
    230. appendTxtLine(buf);
    231. sprintf(buf, "numDetectedObj = %d", head.numDetectedObj);
    232. appendTxtLine(buf);
    233. sprintf(buf, "numTLVs = %d", head.numTLVs);
    234. appendTxtLine(buf);
    235. sprintf(buf, "subFrameNumber = %d", head.subFrameNumber);
    236. appendTxtLine(buf);
    237. }
    238. void readFile() {
    239. char buf[1024];
    240. FILE* file;
    241. file = fopen(FILENAME, "r");
    242. int j = 1;
    243. int i = 0;
    244. if (file == NULL) {
    245. perror("无法打开文件"); // 错误处理
    246. exit(1);
    247. }
    248. while (1) {
    249. // 读取文件内容
    250. size_t bytesRead;
    251. int SIZE = sizeof(Header_t);
    252. while (1) {
    253. bytesRead = fread(&head, sizeof(Header_t), 1, file);
    254. if (head.magicWord[0] == 0x0102 && head.magicWord[1] == 0x0304 && head.magicWord[2] == 0x0506 && head.magicWord[3] == 0x0708)
    255. {
    256. i += 1;
    257. printf("找到个数当前是%d\n", i);
    258. sprintf(buf, "\n-----------------------------------------找到个数当前是%d-----------------------------", i);
    259. appendTxtLine(buf);
    260. break;
    261. }
    262. else
    263. {
    264. fseek(file, -SIZE + 2, SEEK_CUR);
    265. }
    266. }
    267. printf("编号:%d\n", j++);
    268. if (bytesRead > 0) {
    269. // 打印读取的数据
    270. printHead();
    271. }
    272. else {
    273. puts("读 head失败");
    274. break;
    275. }
    276. bytesRead = fread(&TLV_head, sizeof(MmwDemo_output_message_tl), 1, file);
    277. if (bytesRead > 0) {
    278. // 打印读取的数据
    279. printTLV_head(&TLV_head);
    280. }
    281. else {
    282. puts("读 TLV_head失败");
    283. break;
    284. }
    285. bytesRead = fread(&outputStats, sizeof(VitalSignsDemo_OutputStats), 1, file);
    286. if (bytesRead > 0) {
    287. // 打印读取的数据
    288. printOutputStats();
    289. }
    290. else { break; }
    291. bytesRead = fread(&TLV_head1, sizeof(MmwDemo_output_message_tl), 1, file);
    292. if (bytesRead > 0) {
    293. // 打印读取的数据
    294. printTLV_head(&TLV_head1);
    295. }
    296. else {
    297. puts("读 TLV_head1失败");
    298. break;
    299. }
    300. char tmp[1024];
    301. bytesRead = fread(&tmp, 1, TLV_head1.length, file);
    302. printf("跳过数据量:%d\n", TLV_head1.length);
    303. if (bytesRead == 0) {
    304. printf("\n共取到了 %d个数据", j - 1);
    305. fclose(file);
    306. exit(1);
    307. }
    308. /*
    309. for (int i = 0; i < TLV_head1.length; i++) {
    310. bytesRead = fread(&t, 1, 4, file);
    311. if (!bytesRead) {
    312. printf("\n i = %d ,j = %d时失败",i,j);
    313. fclose(file);
    314. exit(1);
    315. }
    316. printf("%04x ", t);
    317. }
    318. */
    319. puts("");
    320. }
    321. puts("关闭文件");
    322. fclose(file); // 关闭文件
    323. }
    324. int main7()
    325. {
    326. readFile();
    327. return 0;
    328. }

  • 相关阅读:
    火山引擎实时、低延时拥塞控制算法的优化实践
    Python3高级特性(一)之切片
    爬虫 — 多线程
    Java SE 10 新增特性
    Maven pom.xml <packaging>pom</packaging> 引发的问题
    混迹互联网,怎么才能写出好简历?
    罗素《为什么我不是基督徒》的后果
    计算机毕业设计之java+ssm高校课程评价系统
    macOS Sonoma 14.1beta3(23B5067a)发布
    同源多页面实时通信之BroadcastChannel实现及简单封装
  • 原文地址:https://blog.csdn.net/laocooon/article/details/133207841