- 论坛徽章:
- 0
|
回复 10# yulihua49
- void GetIpMac(char *_ip, char *_mac)
- {
- system("ipconfig/all >config.txt"); /// ipconfig/all
- ///||||||||||||||||||
- /// C++ FILE
- ///||||||||||||||||||
- ifstream file;
- file.open("config.txt");
- string contents;
- int count = 0;
- while ( !file.eof() )
- {
- count++;
- getline(file,contents);
- //cout/*<<"文件内容->"*/<<contents.c_str()<<endl; /// test print
- int flag_mac = 1;
- if (flag_mac)
- {
- char *pMacE = (char *)strstr(contents.c_str(),"Physical Address");
- char *pMacC = (char *)strstr(contents.c_str(),"物理地址");
- if (NULL != pMacC)
- {
- cout<<"Chinese->"<<pMacC<<endl;
- char *pMac = strstr(pMacC,":");
- pMac += sizeof(":");
- int mac_len = strlen(pMac);
- memcpy(_mac,pMac,mac_len);
- }
- if (NULL != pMacE)
- {
- cout<<"English->"<<pMacE<<endl;
- char *pMac = strstr(pMacE,":");
- pMac += sizeof(":");
- int mac_len = strlen(pMacE);
- memcpy(_mac,pMac,mac_len);
- }
- flag_mac = 0;
- }
- int flag_ip = 1;
- if (flag_ip)
- {
- char *pIpE = (char *)strstr(contents.c_str(),"IP Address");
- char *pIpC = (char *)strstr(contents.c_str(),"IPv4 地址");
- if (NULL != pIpC)
- {
- char *pIp = strstr(pIpC,":");
- pIp += sizeof(":");
- int ip_len = strlen(pIp);
- if (ip_len>13) /// 192.168.1.XXX 纯属扯淡
- ip_len = 13;
- memcpy(_ip,pIp,ip_len);
- //cout<<"_ip "<<pIp<<"ip_len "<<ip_len<<endl; /// cout
- break;
- }
- if (NULL != pIpE)
- {
- //cout<<"English->"<<pLocE<<endl;
- char *pIp = strstr(pIpE,":");
- pIp += sizeof(":");
- int ip_len = strlen(pIp);
- if (ip_len>13) /// 192.168.1.XXX
- ip_len = 13;
- memcpy(_ip,pIp,ip_len);
- break;
- }
- flag_ip = 0;
- }
- }
- cout<<"读文件IP/MAC次数->"<<count<<endl;
- file.close();
- char *str = "config.txt";
- if (0 != remove(str))
- cout<<"Error."<<endl;
- }
复制代码 系统的ipconfig/all命令的输出形式是不一样的、我写到多一半儿、才发现这方法也不对、
那就看看我丑陋的代码吧、
还是遍历网卡的方法可取、 |
|