CN. C is used to extract the CN | IPv4 field from the IP Address Allocation file provided by APNIC ~
#include <stdio.h>#include <string.h> #define MAXLINE 100 int main(){ char readline[MAXLINE]; char readlinecopy[MAXLINE]; char *p; FILE *fpread, *fpwrite; int i; fpread = fopen("APNIC.txt", "r"); fpwrite = fopen("CN.txt", "w"); while(fgets(readline, MAXLINE, fpread) != NULL && readline[0] != '\n'){ strcpy(readlinecopy, readline); printf("readline = %s\n", readline); p = strtok(readline, "|"); i = 0; while(p != NULL){ printf("%s\n", p); if(i==0 && strcmp(p, "apnic") == 0){ i++; // continue; } if(i==1 && strcmp(p, "CN") == 0){ i++; // continue; } if(i==2 && strcmp(p, "ipv4") == 0){ i++; printf("****write readline ****\n"); printf("%s \n", readline); fputs(readlinecopy, fpwrite); // continue; } p = strtok(NULL, "|"); } } fclose(fpread); fclose(fpwrite); return 0;}
Cnip. C is used to extract the source IP address and length of the obtained CN | IPv4 field ~
#include <stdlib.h>#include <stdio.h>#include <string.h> #define MAXLINE 100 int main(){ char readline[MAXLINE]; char writeline[MAXLINE]; char *p; FILE *fpread, *fpwrite; int i; int mask; unsigned int totalmask; fpread = fopen("CN.txt", "r"); fpwrite = fopen("CNIP.txt", "w"); while(fgets(readline, MAXLINE, fpread) != NULL && readline[0] != '\n'){// strcpy(readlinecopy, readline); memset(writeline, 0, MAXLINE); printf("readline = %s\n", readline); p = strtok(readline, "|"); i = 0; while(p != NULL){ printf("%s\n", p); i++; if(i==4){ strcat(writeline, p); strcat(writeline, "|"); // continue; } if(i==5){ strcat(writeline, p); mask = atoi(p); totalmask += mask; fputs(writeline, fpwrite); putc('\n', fpwrite); // continue; } p = strtok(NULL, "|"); } } printf("totalmask = %u\n", totalmask); fclose(fpread); fclose(fpwrite); return 0;}
In fact, this is mainly used to test the functions related to string. h.
Strtok (* sentense, * cutchar) -- used to split sentense using the delimiter cutchar;
Strcpy (* DST, * SRC) -- used to copy the content of the SRC string to the DST string;
Strcat (* sentense, * string) -- used to add a string to the end of sentense;
Atoi (* string) -- function (array to integer) in the <stdlib. h> library, which is used to convert numbers in string format to int format;
In addition, the most important thing is memset (* string, 0, sizeofstring) -- used to assign values to the content of string, which is generally used for clearing (similar to bzero ()).
Among them, apnic.txt is the copy from here. In fact, it was mentioned in the previous article. The two applets ran down to count the total number of IPv4 addresses in China to 334512372.
That's all ~