Home > Back-end >  There are a lot of file information, how to save to the structure?
There are a lot of file information, how to save to the structure?

Time:02-27

Want to open a log file and saving it to the structure, and documents but there are a lot of, can through the for loop when defining structure all the information to the items?
# include
# include
Struct record//record for structure name
{
Char method [255].
Char IP [255];
Char protocol [255].
Char time [255].
Char add [255];
Char port [255].
Char page [255].
Int the status;
Char exploer_info [255].
Struct prams
{
Char key [255].
Char value [255].
};
Int prams_cnt;
};//define structure

Int main (void)
{

The FILE * fp.
Fp=fopen (" access log ", "r +");//open the file
if(! Fp)
{
Printf (" false \ n ");
return -1;//return directly
}
The else {
Printf (" true \ n ");
}//to determine whether a file successfully open the
Struct record items [255].//all records
Int items_cnt=0;//record number
Char line [2048].
Int I=0, x, j, j_pos, j_pos1;
Int lines=0;
Int pos [9], pos1 [9].
Char s='['
Char s1='] ';
Struct record rec.//structure variables
Printf (" log analysis: \ n ");


While ((the fgets (line, 2048, fp))!=NULL)
{
X=strlen (line);
J_pos=0, j_pos1=0;
for(j=0; J{
If (line [j]==s)//here for '[' to begin the location of the
{
Pos [j_pos]=j;
J_pos + +;
}
If (line [j]==s1)//'] 'the location of the end of the
{
Pos1 [j_pos1]=j;
J_pos1 + +;
}
}
//strncpy_s function using strncpy_s (char * str2, int size2, char * str1, int size1);
Strncpy_s (items [s]. Method, sizeof (items [s]. Method), the line + pos [0] + 1, pos1 [0] - pos [0] - 1);
Strncpy_s (items [s]. IP, sizeof (items [s]. IP), the line + pos [1] + 1, pos1 [1] - pos [1] - 1);
Strncpy_s (items [s]. Protocol, sizeof (items [s]. Protocol), line + pos [2] + 1, pos1 [2] - pos [2] 1);
Strncpy_s (items [s]. Time, sizeof (items [s]. Time), the line + pos [3] + 1, pos1 [3] - pos [3] - 1);
Strncpy_s (items [s]. Add, sizeof (items [s]. Add), line + pos [4] + 1, pos1 [4] - pos [4] - 1);
Strncpy_s (items [s]. Page, sizeof (items [s]. Page), line + pos [5] + 1, pos1 [5] - pos [5] 1);
Strncpy_s (items [s]. Port, sizeof (items [s]. Port), the line + pos [6] + 1, pos1 [6] - pos [6] 1);
Strncpy_s (items [s]. Status, sizeof (items [s]. Status), line + pos [7] + 1, pos1 [7] - pos [7] - 1);
Strncpy_s (items [s]. Exploer_info, sizeof (items [s] exploer_info), line + pos [8] + 1, pos1 [8] - pos [8] - 1);
//structure members assignment
}

/* if (line [strlen (line) - 1]=='\ n')
{
Lines++;
} */
//int maxi=0, maxn=0;
Int fclose (fp) FILE *;
Fp=fopen (" access log ", "r +");
Int n=0, m=0;
While (the fgets (line, 2048, fp)!=NULL)
{
Puts (line);
n++;
If (STRNCMP (line, "[GET]", 5)==0)
{
M++;
}//compare two strings, equal STRNCMP function value returns 0, count
}//output log file content and statistical count
Printf (" proportion of output GET request: % f \ n ", 1.0 * m/n);//output percentage;



Int maxi=0, maxn=0;
Char arr [255]={0};
Char vis [255]={0};
/* for (I=0; i{
If (maxn{
Maxn=vis [I];
Maxi=I;
}
} */

for(i=0; i{
Arr [items [I] the status] + +;
Vis [items [I] the status]=1;
If (maxn{
Maxn=vis [I];
Maxi=I;
}
}

Printf (" % lf \ n ", 1.0 * arr [I] + +/I).
Printf (" % lf, "maxi);
Int fclose (fp) FILE *;//close the file
return 0;
}

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: % 31 [^ \ 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);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related