C语言读取特定内容格式地文本文件
答案:3 悬赏:0
解决时间 2021-02-12 00:27
- 提问者网友:纹身骑士
- 2021-02-11 21:07
C语言读取特定内容格式地文本文件
最佳答案
- 二级知识专家网友:ー何必说爱
- 2021-02-11 22:07
使用链表做,文件的记录条数无法确定,数组做有限制
#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 <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
全部回答
- 1楼网友:心与口不同
- 2021-02-11 22:45
#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;
}
- 2楼网友:哭不代表软弱
- 2021-02-11 22:34
#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);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯