从TXT中读取固定格式的数据

dpsy_yes 2016-01-26 04:08:11
在linux下读TXT文本中的内容。
TXT文本的格式如下
groupNumber     = 91;
attData_00 =
{
timeCode = 331294814.750000;
timeString = "2014 07 01 10:20:14.750000";
roll = 0.000000;
pitch = 0.000000;
yaw = 0.000000;
}
attData_01 =
{
timeCode = 331294806.750000;
timeString = "2014 07 01 10:20:06.750000";
roll = 0.000000;
pitch = 0.000000;
yaw = 0.000000;
}

文本共有groupNumber个这样的结构体,需要把这些数据都读出来,存到对应的结构体中。结构体定义如下:
typedef struct  {
double att_timeCode ;
char att_timeString[30];
double att_roll ;
double att_pitch ;
double att_yaw ;

}AttDataStruct;

请问各位大神,有没有比较好的方法,最好是用C语言实现。
...全文
226 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
忘世麒麟 2016-01-28
  • 打赏
  • 举报
回复
你就不能换个方法存数据吗?使用xml将你的数据保存,然后利用现有的工具,比如tinyxml读取(tinyxml是几个读取xml格式的类),这样方便存取,效率还高.同时你还可以掌握一个新的工具,何乐不为? 我就说个建议,要是你的需求规定一定要txt那就没办法了.
苏叔叔 2016-01-27
  • 打赏
  • 举报
回复
模仿赵4老师的做法:

groupNumber     = 2;
attData_00  = 
{
   timeCode   = 331294814.750000;
   timeString = "2014 07 01 10:20:14.750000";
   roll       = 0.000000;
   pitch      = 0.000000;
   yaw        = 0.000000;
}
attData_01  = 
{
   timeCode   = 331294806.750000;
   timeString = "2014 07 01 10:20:06.750000";
   roll       = 0.000000;
   pitch      = 0.000000;
   yaw        = 0.000000;
}

#include <stdio.h>
typedef struct {
	double att_timeCode;
	char   att_timeString[30];
	double att_roll;
	double att_pitch;
	double att_yaw;
}AttDataStruct;
AttDataStruct *data;
FILE *f;
char ln[256];
int st, i, n;
int main(void) {
	f = fopen("data.txt", "r");
	if (f) {
		//先把groupNumber数据读出
		if (NULL == fgets(ln, 256, f))return 0;
		if (1 != sscanf(ln, "groupNumber     = %d", &n))return 0;
		//动态分配空间
		data = (AttDataStruct*)malloc(sizeof(AttDataStruct)*n);
		i = st = 0;
		//正式读入数据
		while (1) {
			if (0 == st) {
				//读入attData_xx
				if (NULL == fgets(ln, 256, f))break;
				//读入{
				if (NULL == fgets(ln, 256, f))break;
				st = 1;
			}
			else if (1 == st) {
				if (NULL == fgets(ln, 256, f))break;
				if (1 == sscanf(ln, "   timeCode   = %lf", &data[i].att_timeCode)) st = 2;
			}
			else if (2 == st) {
				if (NULL == fgets(ln, 256, f))break;
				if (1 == sscanf(ln, "   timeString = \"%30[^\"]s", data[i].att_timeString)) st = 3;
			}
			else if (3 == st) {
				if (NULL == fgets(ln, 256, f))break;
				if (1 == sscanf(ln, "   roll       = %lf", &data[i].att_roll)) st = 4;
			}
			else if (4 == st) {
				if (NULL == fgets(ln, 256, f))break;
				if (1 == sscanf(ln, "   pitch      = %lf", &data[i].att_pitch)) st = 5;
			}
			else {
				if (NULL == fgets(ln, 256, f))break;
				if (1 == sscanf(ln, "   yaw        = %lf", &data[i].att_yaw)) {
					//读入}
					if (NULL == fgets(ln, 256, f))break;
					st = 0;
					if (++i == n)break;
				}
			}
		}
		fclose(f);
		//打印
		printf("groupNumber     = %d;\n", n);
		for (i = 0; i < n; i++) {
			printf("attData_%02d  =\n", i);
			printf("{\n");
			printf("   timeCode   = %.6f;\n", data[i].att_timeCode);
			printf("   timeString = \"%s\";\n", data[i].att_timeString);
			printf("   roll       = %f;\n", data[i].att_roll);
			printf("   pitch      = %f;\n", data[i].att_pitch);
			printf("   yaw        = %f;\n", data[i].att_yaw);
			printf("}\n");
		}
		free(data);
	}
	return 0;
}
ipqtjmqj 2016-01-26
  • 打赏
  • 举报
回复
伪代码: fscanf(fp, "groupNumber=%d ", &groupNumber); for () { fscanf(fp, "attData_%d={", &i); fscanf(fp, "timeCode=%d;", &attData[i].att_timeCode); fscanf(fp, "timeString=\"%d%d%d%d:%d:%d.%d\";", &year, &mon, &day, &hour, &min, &sec, &ssec); sprintf(attData[i].att_timeString, "%04d %02d %02d %02d:%02d:%02d.%06d", year, mon, day, hour, min, sec, ssec); fscanf(fp, "roll=%lf;", &attData[i].att_roll); fscanf(fp, "pitch=%lf;", &attData[i].att_pitch); fscanf(fp, "yaw=%lf;}", &attData[i].att_yaw); }
ipqtjmqj 2016-01-26
  • 打赏
  • 举报
回复
fscanf
赵4老师 2016-01-26
  • 打赏
  • 举报
回复
仅供参考:
//NAME: essaie bla bla
//DIMENSION: 8
//DATA
//1  14  15
//2  11  10
//3  6   4
//4  7   13
//5  9   21
//6  19  3
//7  1   5
//8  8   8
//EOF
//
// 文本文件中可能还含有其他内容,但是需要用到的内容即以上

//比如data.txt:
//NAME: essaie bla bla
//其它内容
//DIMENSION: 8
//其它内容
//DATA
//其它内容
//1  14  15
//其它内容
//2  11  10
//其它内容
//3  6   4
//其它内容
//4  7   13
//其它内容
//5  9   21
//其它内容
//6  19  3
//其它内容
//7  1   5
//其它内容
//8  8   8
//其它内容
//EOF

// 目标是要获取NAME后字串,DIMENSION后数值,以及DATA以下的数值
// 其中NAME就是随便个字句,DIMENSION是城市数量,DATA以下是城市编号,X坐标,Y坐标
// 所有的这些将赋值给一个事先定义好的结构
#include <stdio.h>
#include <string.h>
#define MAXCPL   80   //每行最大字符数
#define MAXCITY  100  //每组数据中DATA最多项数,DIMENSION的最大值
#define MAXNAMEL 32   //NAME最大长度
struct S {
    char NAME[MAXNAMEL+1];
    int  DIMENSION;
    struct D {
        int NO;
        int X;
        int Y;
    } DATA[MAXCITY];
} s;
FILE *f;
int st,n,i;
char ln[MAXCPL];
int main() {
    f=fopen("data.txt","r");
    if (NULL==f) {
        printf("Can not open file data.txt!\n");
        return 1;
    }
    st=0;
    n=0;
    while (1) {
        if (NULL==fgets(ln,MAXCPL,f)) break;
        if (st==0) {
            if (1==sscanf(ln,"NAME: %32[^\n]",s.NAME)) st=1;
        } else if (st==1) {
            if (1==sscanf(ln,"DIMENSION: %d",&s.DIMENSION)) st=2;
        } else if (st==2) {
            if (0==strcmp(ln,"DATA\n")) st=3;
        } else if (st==3) {
            if (3==sscanf(ln,"%d%d%d",&s.DATA[n].NO,&s.DATA[n].X,&s.DATA[n].Y)) {
                n++;
                if (n>=MAXCITY || n>=s.DIMENSION) break;
            }
        }
    }
    fclose(f);
    printf("s.NAME=[%s]\n",s.NAME);
    printf("s.DIMENSION=%d\n",s.DIMENSION);
    for (i=0;i<n;i++) {
        printf("s.DATA[%d].NO,X,Y=%d,%d,%d\n",i,s.DATA[i].NO,s.DATA[i].X,s.DATA[i].Y);
    }
    return 0;
}
//s.NAME=[essaie bla bla]
//s.DIMENSION=8
//s.DATA[0].NO,X,Y=1,14,15
//s.DATA[1].NO,X,Y=2,11,10
//s.DATA[2].NO,X,Y=3,6,4
//s.DATA[3].NO,X,Y=4,7,13
//s.DATA[4].NO,X,Y=5,9,21
//s.DATA[5].NO,X,Y=6,19,3
//s.DATA[6].NO,X,Y=7,1,5
//s.DATA[7].NO,X,Y=8,8,8

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧