I'm very new in c++, my problem is I find a way to use printf
for text formatting in output, but I can't find a way to format the text properly in string variable. My final goal is to save the current mac address in a string variable and compare it with another variable.
This is my code:
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
int main()
{
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
char mac[6];
int ret;
strcpy(s.ifr_name, "enp5s0");
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char) s.ifr_hwaddr.sa_data[0],
(unsigned char) s.ifr_hwaddr.sa_data[1],
(unsigned char) s.ifr_hwaddr.sa_data[2],
(unsigned char) s.ifr_hwaddr.sa_data[3],
(unsigned char) s.ifr_hwaddr.sa_data[4],
(unsigned char) s.ifr_hwaddr.sa_data[5]);
if (mac_as_string == "28:d2:44:55:97:7f") {
}
return 0;
}
return 1;
}
I can't find a way to save mac address as string in mac_as_string variable.