Home > Back-end >  Novice C language help
Novice C language help

Time:02-28

Topic request is to achieve the following six functions, I can now write the code to achieve the first two functions, after the communication with the teacher, I the problems existing in the code should be failed to put the file information stored in a structure, I use strncpy_s functions in your code, want to copy the message to the structure of the items, but the realization of the function of the back, has been stuck here for a long time, still no progress, can see where is wrong?
(1) to list the log file reading and writing;
(2) calculating the log file types the GET request proportion;
(3) to access a status code analysis and statistics, calculating the ratio of all access status code;
(4) analysis of IP address access to the most customers, and statistics the visits;
(5) analysis of commonly used password and statistical password usage;
(6) the user to enter the time, to find the time to visit record and output to a file


# include
# include
Struct record
{
Char method [255].
Char IP [255];
Char protocol [255].
Char time [255].
Char add [255];
Char port [255].
Char page [255].
Char status [255].
Char exploer_info [255].
Char key [255].
Char value [255].
Char prams_cnt [255].
};
Struct record items [2048].//all records
Int items_cnt=0;//record number
Int main (void)
{
The FILE * fp.
Fp=fopen (" access log ", "r +");
if(! Fp) {
Printf (" false \ n ");
}
The else {
Printf (" true \ n ");
}
Int n=0, m=0;
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)
{
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 fclose (fp) FILE *;


Fp=fopen (" access log ", "r +");
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
}

}
Int fclose (fp) FILE *;
Fp=fopen (" access log ", "r +");
Strncpy_s (items [2048], 2048, the line [2048]).
Int maxi=0, maxn=0;
Char arr [255]={0};
Char vis [255]={0};
for(i=0; i{
Arr [items [I] the status [I]] + +;
Vis [items [I] the status [I]]=1;
If (maxn{
Maxn=vis [I];
Maxi=I;
}
}
Printf (" % lf \ n ", 1.0 * arr [I] + +/I).
Printf (" % lf, "maxi);
Int fclose (fp) FILE *;
return 0;
}

CodePudding user response:

Class set myself cheer
  • Related