Created
September 16, 2013 21:37
-
-
Save hrbrmstr/6586928 to your computer and use it in GitHub Desktop.
Take a file with one IP address per line and convert the IP to a long int and output (to stdout) a paired list of IPSTRING,LONGIP (e.g. 1.163.160.100,27500644). I made this since it takes less than 3 seconds to process 4 million IP addresses in C vs the longer times in my other R code and longer times in general in interpreted code. Convert then…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <arpa/inet.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
// | |
// Read in a file with one IP address per line | |
// and output (to stdout) the same list with | |
// the longint version of it. | |
// | |
// No error checking! #youvebeenwarned | |
// | |
// gcc -o batchip2long batchip2long.c | |
// | |
int main(int argc, char *argv[]) { | |
char ip[16] ; | |
FILE *f = fopen(argv[1],"r"); | |
while(fgets(ip, sizeof ip, f) != NULL) { | |
ip[strlen(ip)-1] = '\0'; // take care of eol | |
printf("%s,%ld\n",ip,(long unsigned int)htonl(inet_addr(ip))); | |
} | |
fclose(f) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment