I.p address of machine

i m writing a program which finds the i.p address of the machine.
but it just prints out the first three character of the ifconfig output
but i want to just print my i.p address lik 10.0.0.222 which is in second line after inet addr:
code :

#include<iostream>
#include<cstdlib>
using nammespace std;
int main ()
{
FILE * fp 
fp =popen ("ifconfig eth0","r")
char mystring [100];
if (fb==NULL)perror("error");
else {
if (fgets(mystring , 100, fp)) !=NULL)
puts (mystring);
pclose (fp)
}
return 0;
}
#include <stdio.h>

int main(void)
{
        char buf[512];
        int a, b, c, d;

        FILE *fp=popen("/sbin/ifconfig eth0", "r");

        if(fp == NULL) return(1);

        while(fgets(buf, 512, fp) != NULL)
        {
                if(sscanf(buf, " inet addr:%d.%d.%d.%d", &a, &b, &c, &d)==4)
                        printf("%d.%d.%d.%d\n", a, b, c, d);
        }

        pclose(fp);

        return(0);
}
2 Likes