Home > Software engineering >  C query TXT text specific content and summation
C query TXT text specific content and summation

Time:10-29

I have a TXT file, the content is as follows:

The main DESCRIPTION=
NAME=book ChengDong Road 2967 m2
CLOSED=YES
Generating=705.89 m
ENCLOSED_AREA=0.002967 sq km
Main ChengDong Road=books
635218.689, 3158367.242, - 999999
635218.692, 3158367.083, - 999999

The main DESCRIPTION=
NAME=shen east road 2815 m2
CLOSED=YES
Generating=775.62 m
ENCLOSED_AREA=0.002815 sq km
The main=shen east road
635528.657, 3158186.696, - 999999
635528.657, 3158186.696, - 999999
The main DESCRIPTION=
NAME=shen north road 2248 - square - meter
CLOSED=YES
Generating=761.95 m
ENCLOSED_AREA=0.002248 sq km
The main=shen road
635535.701, 3158553.462, - 999999
635539.669, 3158557.108, - 999999
635537.755, 3158559.637, - 999999

DESCRIPTION=square
NAME=xing-cheng plaza, 1648 - square - meter
CLOSED=YES
Generating=363.97 m
ENCLOSED_AREA=0.001648 sq km

=xing-cheng plaza635206.396, 3158192.482, - 999999
635206.396, 3158192.482, - 999999
635206.372, 3158192.431, - 999999

DESCRIPTION=square
NAME=xing-cheng plaza, 8840 - square - meter
CLOSED=YES
Generating=767.09 m
ENCLOSED_AREA=0.00884 sq km

=xing-cheng plaza635157.547, 3158220.332, - 999999
Behind a lot of similar content,,,,


The file is composed of many of the same paragraph format,
Required functionality is: will the same ENCLOSED_AREA under the DESCRIPTION of this a numerical summation,

I now read through a CFile object pBuff, then how to judge and remove all ENCLOSED_AREA under the same DESCRIPTION?
Thank you all for the

CodePudding user response:

Should have no difficulty, this is the analytical data, you define a data structure, such as
Typedef struct _tagXXXXINFO
{
TCHAR szDescription [XXX];
TCHAR szName [XXX];
.
} XXXINFO;
Parsing the file content, parsing a dynamically allocated a pointer to the object of type, the data properly filled, then the pointer into the list (or use the STL containers such as map, is mainly defined what is the key, add the time before you need to check about whether there is accord with the condition of item, meaning that the same ENCLOSED_AREA) under the same DESCRIPTION, and so on,

CodePudding user response:

Fyi:
//NAME: essaie bla bla 
//DIMENSION: 8
//DATA
14 15//1
//2 November 10
//3 6 4
//4 7 13
//5 September 21
//6 19 3
//7 1 5
//8 8 8
//EOF
//
//text file may also contain other content, but the content of the need to use the above

//such as data. TXT:
//NAME: essaie bla bla
//other content
//DIMENSION: 8
//other content
//DATA
//other content
14 15//1
//other content
//2 November 10
//other content
//3 6 4
//other content
//4 7 13
//other content
//5 September 21
//other content
//6 19 3
//other content
//7 1 5
//other content
//8 8 8
//other content
//EOF

//goal is to get the NAME string, after after DIMENSION values, and the DATA of the following numerical
//which NAME is literally a words, DIMENSION is a number of cities, the DATA below is city number, coordinates X, Y coordinates
//all of these will be assigned to a previously defined structure
#include
#include
# define MAXCPL/80/maximum number of characters per line
100//# define MAXCITY DATA in each set of DATA the multiple number, DIMENSION of the maximum
32//# define MAXNAMEL NAME maximum length
Struct S {
Char NAME [MAXNAMEL + 1];
Int DIMENSION;
Struct D {
Int NO;
Int X;
Int Y;
} DATA [MAXCITY];
} s;
The FILE * f;
Int st, n, I;
Char ln [MAXCPL];
Int main () {
F=fopen (" data. TXT ", "r");
If (NULL==f) {
Printf (" Can not open the file data. TXT! \n");
return 1;
}
St=0;
N=0;
While (1) {
If (NULL==the fgets (ln, MAXCPL, f)) break;
If (st==0) {
If (1==sscanf (ln, "NAME: % 32 [^ \ n]", s.N AME)) st=1;
} else if (st==1) {
If (1==sscanf (ln, "DIMENSION: % d", & amp; Spyware doctor IMENSION 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", & amp; Spyware doctor ATA [n]. NO, & amp; Spyware doctor ATA [n]. X, & amp; Spyware doctor ATA [n]. Y)) {
N++;
If (n>=MAXCITY | | n>=s.d. IMENSION) break;
}
}
}
The fclose (f);
Printf (" s.N AME=] [% s \ n ", s.N AME);
Printf (" s.d. IMENSION=% d \ n ", s.d. IMENSION);
For (I=0; iPrintf (" spyware doctor ATA [% d] NO, X, Y=% d, % d, % d \ n ", I, s.d. ATA [I] NO, s.d. ATA [I] X, s.d. ATA [I] Y);
}
return 0;
}
//s.N AME=[essaie bla bla]
//spyware doctor IMENSION=8
//spyware doctor ATA [0]. NO, X, Y=1,14,15
//spyware doctor ATA [1]. NO, X, Y=2,11,10
//spyware doctor ATA [2]. NO, X, Y=3,6,4
//spyware doctor ATA [3]. NO, X, Y=4,7,13
//spyware doctor ATA [4]. NO, X, Y=5,9,21
//spyware doctor ATA [5]. NO, X, Y=6,19,3
//spyware doctor ATA [6]. NO, X, Y=7,1,5
//spyware doctor ATA [7]. NO, X, Y=8,8,8

  • Related