c - C 中的 ispunct() 有例外吗?
问题描述
我想编写一个代码来检查 MAC 地址的正确性。输入应该看起来像这样D7:6E:F4:30:17:2B
。我正在考虑使用函数isdigit()
和isupper()
. 不知道如何让用户可以写“:”符号并阻止他写其他符号。
if(user input is 13:4F:60:AC:7O:DE)
... all good
if(user input is 14:a]:!o:0L)
... wrong input, retry
编辑 根据@Woodrow Barlow的回答,我编写了该代码:
int mac_address()
{
int is_valid = 1;
printf("MAC ADDRESS:");
fgets(mac_addr, sizeof(mac_addr), stdin);
if (mac_addr[sizeof(mac_addr) - 1] != '\0')
{
is_valid = 0;
}
else if (ether_aton(mac_addr) == NULL)
{
is_valid = 0;
// input isn't recognizable as a MAC address
}
if (is_valid == 1)
{
system("clear");
printf("valid!\n");
printf("%s\n", mac_addr);
return license_menu();
}
else {
printf("invalid!\n");
fflush(stdin);
return 1;
}
}
解决方案
解析 MAC 地址或检查其有效性的最佳方法是使用ether_aton
. MAC 地址可以有多种格式,并且ether_aton
可以用来解析它们。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <netinet/ether.h>
int main(int argc, const char *argv[])
{
char mac_addr[64];
while (true)
{
fgets(mac_addr, sizeof(mac_addr), stdin);
if (mac_addr[sizeof(mac_addr) - 1] != '\0')
{
// input was too long for the buffer
printf("invalid!\n");
}
else if (ether_aton(mac_addr) == NULL)
{
// input isn't recognizable as a MAC address
printf("invalid!\n");
}
else
{
break;
}
}
printf("valid!\n");
printf("%s\n", mac_addr);
return 0;
}
听起来您一次检查一个字符,您想立即拒绝无效字符而不等待完整的输入字符串,并且您特别想拒绝具有小写字母或使用除一个冒号。那准确吗?我会假设你有自己的理由这样做。
ispunct
函数在这里是一个红鲱鱼。没有理由检查给定字符是否是标点字符;你真正想知道的是它是否是一个冒号。具体来说。您可以直接比较它们。
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
bool is_valid(char ch, int i)
{
if ((i + 1) % 3 == 0)
{
return ch == ':';
}
else if (ch >= '0' && ch <= '9')
{
return true;
}
else if (ch >= 'A' && ch <= 'F')
{
return true;
}
return false;
}
int main(int argc, const char *argv[])
{
struct termios old_tio, new_tio;
const int max_len = strlen("00:00:00:00:00:00");
char mac_addr[max_len + 1];
char ch = '\0';
int i = 0;
int ret = 0;
/* need to modify the terminal's underlying settings, because
* by default STDIN is buffered to support backspace, etc.
* by switching to non-buffered input, you lose a lot of basic
* functionality like backspace.
* that's why it's usually recommended to just read in the entire
* line of text and then check if it's valid at the end.
*/
tcgetattr(STDIN_FILENO, &old_tio);
new_tio = old_tio;
new_tio.c_lflag &=(~ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
for (i = 0; i < max_len; i++)
{
ch = getchar();
if (!is_valid(ch, i))
{
printf("\ninvalid!\n");
ret = 1;
goto exit;
}
mac_addr[i] = ch;
}
mac_addr[max_len] = '\0';
printf("\nvalid!\n");
printf("%s\n", mac_addr);
exit:
/* this is important; need to reset the terminal
* settings to their previous value before terminating.
*/
tcsetattr(STDIN_FILENO,TCSANOW,&old_tio);
return ret;
}