【问题标题】:Is there an exception for ispunct() in C?C 中的 ispunct() 有例外吗?
【发布时间】:2019-12-25 18:52:43
【问题描述】:

我想编写一个代码来检查 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;
            }
    }

【问题讨论】:

  • 我不清楚你在问什么,或者你的问题是什么。您可以通过将 ':'':' 进行比较来检查 : 是否为 :。此外,ASCII 是一种文本编码。您可能已经在使用它了。
  • ispunct 检查您不感兴趣的许多其他内容。您应该使用isxdigit(ch) || ch==':'
  • Y4 组件对于一个十六进制字符串来说很奇怪。
  • if (sscanf(s, "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) { /* error*/ }
  • @Matt %x 接受的 syntax 被定义为 strtoul 接受的以 16 为基数的语法,但 C 标准实际上从未说过转换是由strtoul 执行,7.21.6.2p10 表示“如果转换的结果不能在 [destination] 对象中表示,则行为未定义。”我能看到的对该句子的唯一可行解释是,对于strto* 会报告溢出的任何输入,相应的scanf 说明符具有未定义的行为。

标签: c string symbols isalpha


【解决方案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;
}

【讨论】:

  • char mac_addr[max_len +1] { 0 }; 中的错误 ----- 可变大小的对象可能未初始化,我不明白
  • @jadamian 我真的只是想突出 is_valid 功能;我假设您已经有了自己的用户输入代码。但无论如何,我已经用更完整的例子更新了答案。
  • @jadamian 我想强调的是,顶部确实是解析 MAC 地址的最佳方式。主动拒绝使用小写字母或连字符而不是冒号等的 MAC 地址是很奇怪的。
猜你喜欢
  • 2020-04-28
  • 2011-12-29
  • 2017-01-04
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 2018-11-29
  • 1970-01-01
相关资源
最近更新 更多