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

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条回答 默认 最新

  • caozhy
    已采纳
    FILE *fp;
    fp = fopen("x:\\你的文件.txt", "r+");
    char buffer[1000];
    while (!feof(fp))
    {
    fscanf(fp, "%s", &buffer[0]);
    if (strcmp(buffer, "[]BEGIN"))
    buffer就是你要的
    ...
     }
    
    
    点赞 评论

相关推荐