中易网

C语言读取特定内容格式地文本文件

答案:3  悬赏:0  
解决时间 2021-02-12 00:27
C语言读取特定内容格式地文本文件
最佳答案
使用链表做,文件的记录条数无法确定,数组做有限制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_COUNT 4096

#define LINE_PART1 32
#define LINE_PART2 128

#define G01_ARRAY "G01"
#define G02_ARRAY "G02"

typedef struct G01
{
int linenum;
float x;
float y;
}G01;

typedef struct G01_node
{
G01 g01;
struct G01_node *pg01_node;
}G01_node;

typedef struct G01_list
{
G01_node *pg01_first;
G01_node *pcurr_node;
int size;
}G01_list;

typedef struct G02
{
int linenum;
float x;
float y;
float i;
float j;
}G02;

typedef struct G02_node
{
G02 g02;
struct G02_node *pg02_node;
}G02_node;

typedef struct G02_list
{
G02_node *pg02_first;
G02_node *pcurr_node;
int size;

}G02_list;

int parseG01String(char *linecontent, G01_list *arr);
int parseG02String(char *linecontent, G02_list *arr);
int parseString(char *linecontent);

G02_list pg02_list = {NULL, 0};
int errorcounter_g02 = 0;

G01_list pg01_list = {NULL, 0};
int errorcounter_g01 = 0;

int currnum = 0;

int main(int argc, char **argv)
{
if(argc!=2)
{
printf("??????\n");
return -1;
}

FILE *fs = NULL;

if(!(fs = fopen(argv[1], "r+")))
{
printf("??????\n");
return -1;
}

char linecontent[LINE_COUNT] = {0};

while(fgets(linecontent, LINE_COUNT, fs))
{
//printf("%s:the linecontent is:%s\n", __FUNCTION__, linecontent);

++currnum;
parseString(linecontent);
}

printf("the result is:\n");
printf("G01 array counter:%d, follow data is\n", pg01_list.size);

G01_node *ptemp1 = pg01_list.pg01_first;
while(ptemp1)
{
printf("line:%d\t(%.3f\t, %.3f)\n", ptemp1->g01.linenum, ptemp1->g01.x, ptemp1->g01.y);
ptemp1 = ptemp1->pg01_node;
}

printf("\n\nG01 array counter:%d, follow data is\n", pg02_list.size);
G02_node *ptemp2 = pg02_list.pg02_first;
while(ptemp2)
{
printf("line:%d\t(%.3f\t, %.3f\t, %.3f\t, %.3f)\n", ptemp2->g02.linenum, ptemp2->g02.x, ptemp2->g02.y, ptemp2->g02.i, ptemp2->g02.j);
ptemp2 = ptemp2->pg02_node;
}

return 0;
}

//return: -1:paremeter is null, -2:format of paremeter is invalid, -3:mem alloc fail, 0: success
int parseG01String(char *linecontent, G01_list *arr)
{
int ret = 0;
if(!linecontent)
{
return -1;
}

G01_node *p1 = (G01_node*)malloc(sizeof(G01_node));

memset((void *)p1, 0, sizeof(G01_node));
if(!p1)
{
return -3;
}

if(sscanf(linecontent, "X%f Y%f", &p1->g01.x, &p1->g01.y) != 2)
{
ret = 2;
free((void *)p1);
p1 = NULL;
}
else
{
p1->g01.linenum = currnum;

if(!arr->size)
{
arr->pg01_first = p1;
arr->pcurr_node = p1;
}
else
{
arr->pcurr_node->pg01_node = p1;
arr->pcurr_node = arr->pcurr_node->pg01_node;
}

++(arr->size);
}

if(ret != 0)
{
printf("%s:linecontent in %d:%s error.\n", __FUNCTION__, currnum, linecontent);
}

return ret;

}

//return: -1:paremeter is null, -2:format of paremeter is invalid, -3:mem alloc fail, 0: success
int parseG02String(char *linecontent, G02_list *arr)
{
int ret = 0;
if(!linecontent)
{
return -1;
}

G02_node *p1 = (G02_node*)malloc(sizeof(G02_node));

memset((void *)p1, 0, sizeof(G02_node));
if(!p1)
{
return -3;
}

if(sscanf(linecontent, "X%f Y%f I%f J%f", &(p1->g02.x), &(p1->g02.y), &(p1->g02.i), &(p1->g02.j)) != 4)
{
//printf("%s:linecontent in %f:%f:%f:%f .\n", __FUNCTION__, p1->g02.x, p1->g02.y, p1->g02.i, p1->g02.j);

ret = 2;
free((void *)p1);
p1 = NULL;
}
else
{
p1->g02.linenum = currnum;

if(!arr->size)
{
arr->pg02_first = p1;
arr->pcurr_node = p1;
}
else
{
arr->pcurr_node->pg02_node = p1;
arr->pcurr_node = arr->pcurr_node->pg02_node;
}

++(arr->size);
}

if(ret != 0)
{
printf("%s:linecontent in %d:%s error.\n", __FUNCTION__, currnum, linecontent);
}

return ret;

}

//return: -1:paremeter is null, -2:format of paremeter is invalid, 0: success
int parseString(char *linecontent)
{
int ret = 0;
if(!linecontent)
{
return -1;
}

char linepart1[LINE_PART1] = {0};
char linepart2[LINE_PART2] = {0};

if(sscanf(linecontent, "%s", linepart1) != 1)
{
return -2;
}

char *pchar = strstr(linecontent, linepart1) + strlen(linepart1);
while(*pchar == ' ' || *pchar == '\t')
pchar++;

strcpy(linepart2, pchar);

//printf("%s:%s -- %s\n", __FUNCTION__, linepart1, linepart2);

if(!strcmp(G01_ARRAY, linepart1))
{
if(ret = parseG01String(linepart2, &pg01_list))
{
return ret;
}
}
else if(!strcmp(G02_ARRAY, linepart1))
{
if(ret = parseG02String(linepart2, &pg02_list))
{
return ret;
}
}
else
{
if(ret != 0)
{
printf("%s:linecontent in %d:%s error.\n", __FUNCTION__, currnum, linecontent);
}

return -2;
}

return ret;

}

使用:
uc: ./a.out 1.txt
vc: 1.exe 1.txt
执行程序和文本放在同一个目录内
1.txt文本内容
G02 X-26.414 Y21.386 I28.448 J-18.611
G01 X-23.305 Y18.869
G02 X25.585 Y15.659 I23.311 J-18.877
G01 X27.153 Y12.745
G02 X19.932 Y-22.369 I-26.992 J-12.749
全部回答
#include <stdio.h> #include <string.h> #define MAX 100 typedef struct { double X,Y; }sg1; typedef struct { double X,Y,I,J; }sg2; int main() { FILE *fp; sg1 G1[MAX]; sg2 G2[MAX]; int i=0,j=0; int cnt1=0,cnt2=0; char s[256]; if((fp=fopen("1.txt","r"))==NULL) {printf("打开文件出错.\n");return 1;} memset(s,'\0',sizeof(s)); fgets(s,256,fp); while(!feof(fp)) { if(strstr(s,"G01")!=NULL)//匹配G01 { sscanf(s,"G01 X%lf Y%lf",&G1[i].X,&G1[i].Y); i++; cnt1++; } if(strstr(s,"G02")!=NULL)//匹配G02 { sscanf(s,"G02 X%lf Y%lf I%lf J%lf",&G2[j].X,&G2[j].Y,&G2[j].I,&G2[j].J); j++; cnt2++; } if(cnt1>=MAX||cnt2>=MAX) {printf("超出数组最大定义.\n");return 2;} fgets(s,256,fp); } printf("G01:\n"); for(i=0;i<cnt1;i++) printf("X=%g,Y=%g\n",G1[i].X,G1[i].Y); printf("G02:\n"); for(j=0;j<cnt2;j++) printf("X=%g,Y=%g,I=%g,J=%g\n",G2[j].X,G2[j].Y,G2[j].I,G2[j].J); return 0; }
#include #include #include //函数返回fname指定文件的全部内容,如果打不开文件,则返回null,并显示打开文件错误 char *getfileall(char *fname){ file *fp; char *str; char txt[1000]; int filesize; if ((fp=fopen(fname,"r"))==null){ printf("打开文件%s错误\n",fname); return null; } fseek(fp,0,seek_end); filesize = ftell(fp); str=(char *)malloc(filesize); str[0]=0; rewind(fp); while((fgets(txt,1000,fp))!=null){ strcat(str,txt); } fclose(fp); return str; } int main(){ char *p; char *fname="d:\\temp.txt"; p=getfileall(fname); if (p!=null) puts(p); }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
什么是丹宁布?
2003年6点档播放的韩剧有哪些两姐妹变妯娌
恒达二手车在什么地方啊,我要过去处理事情
网络男歌手排行榜第一的是谁谢谢了,大神帮忙
为什么奔驰c63拆掉前保险杠后,前后雷达不报
台湾自助游什么时间去比较好?
高中时期喜欢一个人,该怎么办
我女朋友的朋友不知道我 经常的晚上跟我女朋
在韩国买奢侈品划算还是去香港买?
拼音怎么读音g
急 !cooledit 不能录音!!!是为什么?
古道茶楼地址有知道的么?有点事想过去
大伟筋饼骨头王地址在哪,我要去那里办事
胖丽超市怎么去啊,有知道地址的么
我想在现实中把我的淘宝店印成资料发放 请问
推荐资讯
建设银行的定期储蓄自动转存是怎么规定的
苏州益达冷风机厂我想知道这个在什么地方
爱心五谷养生粥(大连职业技术学院店)我想知道
你已经说过你不爱我了,不管是单恋还是相爱都
昂立国际教育普宁市流沙赤华路分校在什么地方
宝享通快餐饭店在哪里啊,我有事要去这个地方
求家教的漫画、同人小说
求一则我国古人信守承诺的故事
我国线导鱼雷的状况怎样
新兵入伍安时给的是什么费用
隆顺百货狮岭分店地址在什么地方,想过去办事
硬盘线怎么插上面有sata 的两根
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?