maocz 2019-06-06 16:38 采纳率: 100%
浏览 620
已采纳

C++高效处理TXT文本,并写入结构体数组的优秀实例?

C++读取TXT文本文件[BGIN]和[END]中相关有用数据,将数据写入结构体数组(如下),

struct Vpdata
{
chart PT[5]; //X9
string ActiveTiP; //B1S8
float Retract; //3.000000
float Tolerance_Upper; //0.020
float Tolerance_Lower; //-0.020
double X; //46.0978
double Y; //449.5917
double Z; //264.0902
double I; //0.8906
double J; //0.1080
double K; //0.4417
};

txt格式如下,行数量未知;
... ...
... ...
[BEGIN]
X9:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=46.0978,449.5917,264.0902,0.8906,0.1080,0.4417
X1:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=79.8739,529.1789,317.0000,0.0000,0.0000,1.0000
X2:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=40.7599,508.9691,292.0000,0.0000,0.0000,1.0000
X3:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=17.3222,509.0666,297.4902,0.000,-0.5924,0.8056
X4:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=36.4060,486.6219,281.6236,0.000,-0.5748,0.8183
X5:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=44.2144,469.7959,273.7131,0.0000,-0.9962,0.0872
X6:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=147.8952,464.1894,265.9202,0.00,-0.5585,0.8295
X7:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=139.8095,463.2058,272.1759,0.86,0.1464,0.4886
X8:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=147.0377,455.5513,266.9044,0.8916,0.1626,0.4226
X10:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=177.1025,476.6855,266.5594,0.0000,-0.9986,0.0523
X11:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=158.5258,461.6525,254.3172,1.0000,0.0000,0.0000
X12:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=158.5258,450.9872,249.6415,1.0000,0.0000,0.0000
X13:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=204.6803,462.8675,267.9680,-0.9659,0.0000,0.2588
X14:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=201.7029,447.5408,256.8562,-0.9659,0.0000,0.2588
X15:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=172.4805,433.3200,246.7664,0.0000,0.9986,0.0523
X16:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=154.5043,430.8859,253.7178,0.0000,-0.4304,0.9027
X17:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=155.3791,419.7767,248.7889,0.0000,-0.6482,0.7614
X18:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=157.0619,400.7633,232.8402,0.0000,-0.6369,0.7710
X19:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=157.7367,383.6450,218.8977,0.0000,-0.6260,0.7798
X20:M=B1S8,RA=3.000000,MSG=0.020,-0.020,MKJYGF=157.6545,365.7935,204.7889,0.0000,-0.6140,0.7893
... ...
[END]

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-06-06 17:35
    关注
    FILE *fp;
    fp = fopen("x:\\你的文件.txt", "r+");
    char buffer[1000];
    while (!feof(fp))
    {
    fscanf(fp, "%s", &buffer[0]);
    if (strcmp(buffer, "[]BEGIN"))
    buffer就是你要的
    ...
     }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。