我正在尝试将Linux中的ARP表格转换为下面发布代码的数组。我总是在变量ip和mac中获取地址,但是当分配给数组时,它只显示一些疯狂的数字。我做错了吗? (我不是很熟悉编程)
struct ARP_entry
{
char IPaddr;
char MACaddr;
char ARPstatus;
int timec;
};
static struct ARP_entry ARP_table[ARP_table_vel];
void getARP()
{
int i=0;
const char filename[] = "/proc/net/arp";
char ip[16], mac[18], output[128];
FILE *file = fopen(filename, "r");
if ( file )
{
char line [ BUFSIZ ];
fgets(line, sizeof line, file);
while ( fgets(line, sizeof line, file) )
{
char a,b,c,d;
if ( sscanf(line, "%s %s %s %s %s %s", &ip, &a, &b, &mac, &c, &d) < 10 )
{
if ( ARP_table_vel > i)
{
ARP_table[i].IPaddr = ip;
ARP_table[i].MACaddr = mac;
ARP_table[i].ARPstatus = STATUS_CON;
i++;
}
}
}
}
else
{
perror(filename);
}
答案 0 :(得分:0)
您需要修复结构并将char
变量转换为char
数组:
struct ARP_entry
{
char IPaddr[16];
char MACaddr[18];
char ARPstatus;
int timec;
};
然后,您需要对数据进行适当的复制,以便保存它们:
if ( ARP_table_vel > i)
{
snprintf(ARP_table[i].IPaddr, 16, "%s", ip);
snprintf(ARP_table[i].MACaddr, 18, "%s", mac);
ARP_table[i].ARPstatus = STATUS_CON;
i++;
}
最后,ARP表有一个标题,所以你需要丢弃第一行。