d_名称); //跳过父文件夹和当前文件夹 如果(设备==“| |设备==”){ 继续; } 目录列表。推回(设备); } closedir(dir); } } //遍历设备并找到它们的mac地址 std::vector networkinterfacelist\u类::创建\u设备(){ 对于(std::vector::iterator it=dir_list.begin();it!=dir_list.end();++it){ if((dir2=opendir(“/sys/class/net”)!=NULL){ if((dir=opendir(it->c_str())!=NULL){ //打开地址文件,将mac地址保存在行中 填写公开信(“地址”); 而(!infle){ getline(填充,行); } //创建新的networkinterface类并将其推送到列表中 网络接口_类obj(*it,线路); 列表。推回(obj); } } } 退货清单; } //迭代网络设备列表并打印其名称和mac地址 void networkinterfacelist_class::to_string(){ 对于(std::vector::iterator it=list.begin();it!=list.end();++it){ (*it.to_string(); } },c++,C++" /> d_名称); //跳过父文件夹和当前文件夹 如果(设备==“| |设备==”){ 继续; } 目录列表。推回(设备); } closedir(dir); } } //遍历设备并找到它们的mac地址 std::vector networkinterfacelist\u类::创建\u设备(){ 对于(std::vector::iterator it=dir_list.begin();it!=dir_list.end();++it){ if((dir2=opendir(“/sys/class/net”)!=NULL){ if((dir=opendir(it->c_str())!=NULL){ //打开地址文件,将mac地址保存在行中 填写公开信(“地址”); 而(!infle){ getline(填充,行); } //创建新的networkinterface类并将其推送到列表中 网络接口_类obj(*it,线路); 列表。推回(obj); } } } 退货清单; } //迭代网络设备列表并打印其名称和mac地址 void networkinterfacelist_class::to_string(){ 对于(std::vector::iterator it=list.begin();it!=list.end();++it){ (*it.to_string(); } },c++,C++" />

访问要输出的向量迭代器元素 我对C++非常陌生,我正在尝试创建一个实用工具,它找到所有的网络设备并列出它们的名称和MAC地址。我已经得到了所有要编译的东西,但是当我运行to_string()方法时,我没有得到任何到终端的输出。我认为我正在访问迭代器对象,并对其错误地调用了to_string() //networkinterfacelist_class.cpp #include "networkinterfacelist.h" //get list of network interfaces and push them into a vector list networkinterfacelist_class::networkinterfacelist_class(){ if ((dir = opendir ("/sys/class/net")) != NULL){ while ((ent = readdir (dir)) != NULL) { std::string device(ent->d_name); //skips the parent and current folder if(device == "." || device == ".."){ continue; } dir_list.push_back(device); } closedir (dir); } } //iterate through the devices and find their mac addresses std::vector<networkinterface_class> networkinterfacelist_class::create_device(){ for( std::vector<std::string>::iterator it = dir_list.begin(); it != dir_list.end(); ++it){ if ((dir2 = opendir ("/sys/class/net")) != NULL){ if ((dir = opendir (it->c_str())) != NULL){ //opens the address file saves mac address in line inFile.open("address"); while(!inFile){ getline(inFile, line); } //creates a new networkinterface class and pushes it onto a list networkinterface_class obj( *it , line); list.push_back(obj); } } } return list; } //iterates over the list of network devices and prints their name and mac address void networkinterfacelist_class::to_string(){ for(std::vector<networkinterface_class>::iterator it = list.begin(); it != list.end(); ++it){ (*it).to_string(); } } //网络接口列表\u class.cpp #包括“networkinterfacelist.h” //获取网络接口列表并将其推送到向量列表中 networkinterfacelist_类::networkinterfacelist_类(){ if((dir=opendir(“/sys/class/net”)!=NULL){ while((ent=readdir(dir))!=NULL){ 字符串设备(ent->d_名称); //跳过父文件夹和当前文件夹 如果(设备==“| |设备==”){ 继续; } 目录列表。推回(设备); } closedir(dir); } } //遍历设备并找到它们的mac地址 std::vector networkinterfacelist\u类::创建\u设备(){ 对于(std::vector::iterator it=dir_list.begin();it!=dir_list.end();++it){ if((dir2=opendir(“/sys/class/net”)!=NULL){ if((dir=opendir(it->c_str())!=NULL){ //打开地址文件,将mac地址保存在行中 填写公开信(“地址”); 而(!infle){ getline(填充,行); } //创建新的networkinterface类并将其推送到列表中 网络接口_类obj(*it,线路); 列表。推回(obj); } } } 退货清单; } //迭代网络设备列表并打印其名称和mac地址 void networkinterfacelist_class::to_string(){ 对于(std::vector::iterator it=list.begin();it!=list.end();++it){ (*it.to_string(); } }

访问要输出的向量迭代器元素 我对C++非常陌生,我正在尝试创建一个实用工具,它找到所有的网络设备并列出它们的名称和MAC地址。我已经得到了所有要编译的东西,但是当我运行to_string()方法时,我没有得到任何到终端的输出。我认为我正在访问迭代器对象,并对其错误地调用了to_string() //networkinterfacelist_class.cpp #include "networkinterfacelist.h" //get list of network interfaces and push them into a vector list networkinterfacelist_class::networkinterfacelist_class(){ if ((dir = opendir ("/sys/class/net")) != NULL){ while ((ent = readdir (dir)) != NULL) { std::string device(ent->d_name); //skips the parent and current folder if(device == "." || device == ".."){ continue; } dir_list.push_back(device); } closedir (dir); } } //iterate through the devices and find their mac addresses std::vector<networkinterface_class> networkinterfacelist_class::create_device(){ for( std::vector<std::string>::iterator it = dir_list.begin(); it != dir_list.end(); ++it){ if ((dir2 = opendir ("/sys/class/net")) != NULL){ if ((dir = opendir (it->c_str())) != NULL){ //opens the address file saves mac address in line inFile.open("address"); while(!inFile){ getline(inFile, line); } //creates a new networkinterface class and pushes it onto a list networkinterface_class obj( *it , line); list.push_back(obj); } } } return list; } //iterates over the list of network devices and prints their name and mac address void networkinterfacelist_class::to_string(){ for(std::vector<networkinterface_class>::iterator it = list.begin(); it != list.end(); ++it){ (*it).to_string(); } } //网络接口列表\u class.cpp #包括“networkinterfacelist.h” //获取网络接口列表并将其推送到向量列表中 networkinterfacelist_类::networkinterfacelist_类(){ if((dir=opendir(“/sys/class/net”)!=NULL){ while((ent=readdir(dir))!=NULL){ 字符串设备(ent->d_名称); //跳过父文件夹和当前文件夹 如果(设备==“| |设备==”){ 继续; } 目录列表。推回(设备); } closedir(dir); } } //遍历设备并找到它们的mac地址 std::vector networkinterfacelist\u类::创建\u设备(){ 对于(std::vector::iterator it=dir_list.begin();it!=dir_list.end();++it){ if((dir2=opendir(“/sys/class/net”)!=NULL){ if((dir=opendir(it->c_str())!=NULL){ //打开地址文件,将mac地址保存在行中 填写公开信(“地址”); 而(!infle){ getline(填充,行); } //创建新的networkinterface类并将其推送到列表中 网络接口_类obj(*it,线路); 列表。推回(obj); } } } 退货清单; } //迭代网络设备列表并打印其名称和mac地址 void networkinterfacelist_class::to_string(){ 对于(std::vector::iterator it=list.begin();it!=list.end();++it){ (*it.to_string(); } },c++,C++,还有我的networkinterface类 //networkinterface_class.cpp #include "networkinterface.h" networkinterface_class::networkinterface_class(std::string device, std::string macaddress){ name = device; mac = macaddress; } std::string networkinterface_class::

还有我的networkinterface类

//networkinterface_class.cpp
#include "networkinterface.h"

networkinterface_class::networkinterface_class(std::string device, std::string macaddress){
  name = device;
  mac = macaddress;
}

std::string networkinterface_class::get_name(){
  return name;
}

std::string networkinterface_class::get_mac(){
  return mac;
}

void networkinterface_class::to_string(){
  std::cout << "Name: " << networkinterface_class::get_name() << "\tMAC: " << networkinterface_class::get_mac() << std::endl;
}
//网络接口\u class.cpp
#包括“networkinterface.h”
网络接口类::网络接口类(std::字符串设备,std::字符串macaddress){
名称=设备;
mac=macaddress;
}
std::string networkinterface_class::get_name(){
返回名称;
}
std::string networkinterface_class::get_mac(){
返回mac;
}
void networkinterface_class::to_string(){

std::cout您需要为设备定义整个路径

dir_list.push_back(device); // it pushes only d_name of dirent == filename
在上面的一行中,您只从
/sys/class/net
推送设备名称,因此当您想要读取此设备时,您需要通过将/sys/class/net/与设备名称连接起来来创建整个路径

if ((dir = opendir ( ("/sys/class/net/" + *it).c_str() )) != NULL){
                                          ^^^ get device name as string
而不是

if ((dir = opendir (it->c_str())) != NULL){ // you pass only device without full path
如果要打开
地址
文件,请执行相同操作:

inFile.open("/sys/class/net/" + *it + "/address");

现在您可以阅读此文件的内容了。

您检查过列表是否为空吗?请注意,
create\u device
返回一份副本,因此更改结果不会更改
列表
成员。首先添加一个
cout@FrançoisAndrieux,我刚刚检查过,列表为空,有没有关于如何修复它的建议?@Jeffrey我唯一的建议将附加一个调试器并逐步执行生成列表的代码。您似乎有一些全局向量,或者它们可能是数据成员?无论哪种方式,我都看不到它们在哪里声明,尝试猜测也没有什么意义。尝试将您的代码缩减为-这意味着它必须是最小的完整且自包含的pro这就是问题所在。