Splits an IP address string into four integer forms

Source: Internet
Author: User

/** file:main.c* author:liutingting*/#include<stdio.h>#include<stdlib.h>#include<Winsock2.h>#include<string.h>#include<time.h>#include<Ws2tcpip.h>#pragmaComment (lib, "Ws2_32.lib")intMainintargcChar**argv) {intI,j;u_long Count=0; U_short Datalen=0;intSzfh=0; U_long Sumlen=0; U_long Qc_sumlen=0;Char*filepath[3];filepath[0]="C:\\users\\administrator\\desktop\\1208test\\open.txt";Charstorefile[]="C:\\users\\administrator\\desktop\\store.txt";Charsip[ -],dip[ -],id[Ten];intip1=0, ip2=0, ip3=0, ip4=0; FILE*fp[3],* out;//can be used to open multiple files continuously for(i=0;i<1; i++){//itoa (i,first,10); while(Fp[i] = fopen (Filepath[i],"R")) ==NULL) {printf ("can't open file \ n"); exit (0);} Remove (storefile); out=fopen (StoreFile,"A +");Charch;Charline[ -]; while(Fgets (line, -, fp[i])! =NULL) {if(Feof (Fp[i])) {//prevent the last line from being read more Break;}Else{sscanf (line,"%s%s%s", Id,sip,dip);//Write Filefprintf ( out,"%s%s%s\n", Id,sip,dip);p rintf ("line=%s\n", line);p rintf ("id=%s sip=%s dip=%s\n", Id,sip,dip);intIpi=0;intK;ip1=0, ip2=0, ip3=0, ip4=0; for(k=0; sip[k]!=' /'; k++){if(sip[k]=='.') {IPI++;Continue;}Switch(IPI) { Case 0: Ip1=ip1*Ten+ (sip[k]- -); Break; Case 1: Ip2=ip2*Ten+ (sip[k]- -); Break; Case 2: IP3=ip3*Ten+ (sip[k]- -); Break; Case 3: IP4=ip4*Ten+ (sip[k]- -); Break; }} printf ("%d %d%d%d\n", IP1,IP2,IP3,IP4);}}} Fclose (fp[0]); Fclose ( out); system ("Pause");returnexit_success;} 

Operation Result:

Splits an IP address string into four integer forms

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.