#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
char output[17]={0};
const char* change_mac(char ch[100])
{
int i = 0;
int k = 0;
bool flag = false;
while(i < strlen(ch))
{
flag = false;
int j = 0;
while (j < 2)
{
if(ch[i] == ':' || strlen(ch) == i)
{
if(k != 0)
{
char temp = output[k-1];
output[k-1] = '0';
output[k] = temp;
}
flag = true;
}
else
{
output[k] = ch[i];
}
i++;
j++;
k++;
}
if(i < strlen(ch) && memchr(ch, ':', strlen(ch)))
{
output[k] = ':';
output[k+1] = '\0';
}
else{
output[k] = ch[i];
}
k++;
if(!flag)
{
i++;
}
}
output[k-1] = 0;
return output;
}
int main(int argc,char **argv)
{
char name[]="";
char ch[100] = "0:0:0:0:0:0";
if(sscanf(argv[1],"%s",ch) == 1)
{
name=change_mac(ch);
}
return 0;
}
/我想獲得MAC爲CMD行認爲把它變成正確的格式,並在存儲這個「名」變量,這樣我可以用它進一步這裏它顯示了一些類型轉換錯誤/字符串在C:給一些邏輯錯誤
/*執行:./a.out 0:C:F4:DR:4R:1 */
你必須在'ch []'中檢查':'if in,if if(i!= sizeof(ch)){output [k] =':'; }'對於第二個 –
在C中沒有try/catch結構,但是在C++中。我不會建議使用它,除了嚴重失敗之外,因爲它非常昂貴。 –
AndreasGrapentin說真的..如果你想實現try-catch一段時間,這個鏈接將是有用的[我如何用C中的消息的可嵌套try-catch-finally語句實現異常(http://stackoverflow.com/問題/ 1779189/how-do-i-implement-exceptions-with-nestable-try-catch-finally-statement-with-mes)和http://www.nicemice.net/cexcept/ –