1

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.

nim4n
  • 1,813
  • 3
  • 21
  • 36
  • 1
    _" My final goal is to save the current mac address in a string variable and compare it with another variable."_ Well, you coud use `std::ostringstream` and obey what I've said [here](http://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout/19562163#19562163). – πάντα ῥεῖ May 13 '17 at 11:12
  • 1
    Any reason you're not using `std::array`? Then you don't have to use strings, since operator equal is defined for it. – Jonas May 13 '17 at 11:15
  • @Jonas That's a pretty neat idea. – πάντα ῥεῖ May 13 '17 at 11:16
  • @jonas I'm a very new in c++ so I used a sample code I found for getting the mac address and now I have a problem with saving the result in string variable. – nim4n May 13 '17 at 11:17
  • _@nim4n_ If you want a sample code for creating a string, I could write an answer. Though, if it's only about comparison, what @Jonas proposed is the better idea. You may improve your question to extend where and what you want to compare actually. – πάντα ῥεῖ May 13 '17 at 11:19
  • @πάνταῥεῖ yes I just want to save the mac address to a string, I can print the format I want with printf but I couldn't find a way to put it in string – nim4n May 13 '17 at 11:22

1 Answers1

1

Regarding the text formatting and creating a string variable the easiest way to go is using a std::ostringstream:

std::ostringstream macbuilder;

macbuilder << std::hex 
           << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[0]) << ':'
           << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[1]) << ':'
           << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[2]) << ':'
           << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[3]) << ':'
           << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[4]) << ':'
           << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[5];
std::string macaddr = macbuilder.str();

But if you just want to have a variable that is comparable with another one std::array<uint8_t,6> is the way to go:

std::array<uint8_t,6> macaddr {
    s.ifr_hwaddr.sa_data[0] ,
    s.ifr_hwaddr.sa_data[1] ,
    s.ifr_hwaddr.sa_data[2] ,
    s.ifr_hwaddr.sa_data[3] ,
    s.ifr_hwaddr.sa_data[4] ,
    s.ifr_hwaddr.sa_data[5] ,
    };

As mentioned std::array provides the operator==() and you could simply use that to compare with another variable created the same way.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I get this error: mac.cpp:27:68: warning: missing terminating ' character << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[0]) << ':` ^ mac.cpp:27:12: error: missing terminating ' character << std::setw(2) << unsigned(s.ifr_hwaddr.sa_data[0]) << ':` ^ – nim4n May 13 '17 at 11:31
  • @nim4n Sorry, fixed that. I was typing too fast. – πάντα ῥεῖ May 13 '17 at 11:33
  • in the first code I'm getting this error: expected primary-expression before ‘unsigned’ << std::setw(2) << unsigned char (s.ifr_hwaddr.sa_data[0]) << ':' – nim4n May 14 '17 at 05:03
  • and for the second as I edit my post my final goal is to compare the output with a simple string, should I change my string to the array too? – nim4n May 14 '17 at 05:06