Home > Mobile >  Use inet_pton for IP address conversion from a text file not working
Use inet_pton for IP address conversion from a text file not working

Time:10-04

I am trying to convert a range of ip addresses contained in a text file using inet_pton function. I need to get ip adresses strings from the text file and pass as src address parameter to inet_pton(). After i check inet_pton() return 0 not 1. Please, i would be grateful for any help


    whitelist= fopen("/home/...../whitlist.txt","r");

    while(fgets(line, sizeof(line), whitelist) ) {

        printf("%s", line);
    }
    if(inet_pton (AF_INET,line, &a)==1){
           printf("1");
       } else if(inet_pton (AF_INET,line, &a)==0){
           printf("0\n");
       } else {
           printf("-1");
       }

CodePudding user response:

I do not know the function but it seems you need to remove the new line character. For example

while(fgets(line, sizeof(line), whitelist) ) {
    line[ strcspn( line, "\n" ) ] = '\0';
    puts( line);
}
  • Related