Two file string applets (cn_ipv4 extract)

Source: Internet
Author: User
Tags strtok

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.