Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created September 16, 2013 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrbrmstr/6586928 to your computer and use it in GitHub Desktop.
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…
#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