c - 从重定向为输入的文件中验证日期

标签 c validation date file-handling stdio

首先,这是一道作业题。

具有格式为 mm/dd/yyyy 的日期列表的文件将被重定向为命令提示符的输入。这是一个示例 ./main.out < file.txt。对于我找到的每个有效日期,我想将其写入输出文件。


我的教授给出了构成无效日期的标准。

1.) 如果有任何非数字字符,如 12/4A/199A5。

2.) 如果他们缺少正斜杠或有超过 2 个正斜杠。

3.) 如果它们是像 12/4/1995.3 这样的 float

4.) 如果它们是不正确的日期,如 2/29/1973,因为 29 意味着该年应该是闰年,但 1973 不能被 4 或 400 整除。

5.) 如果没有日期(空字符串)或部分缺失,如 4/3/。


这里是有效日期的例子。

1.) 带有前导或尾随空格的日期,例如 12/23/1694

2.) 带有前导零的日期,例如 004/030/2000

3.) 月份数在 1 到 12 之间的任何日期,正确的日长(这取决于月份)和年份可以是正数、负数或零。


这是我到目前为止所做的,只有必要的相关功能。

int writeToFile()
{
    FILE *outputFile;
    char buffer[BUFFERSIZE];
    int month = 0, days = 0, year = 0;
    int isValidFormat = 0, isValidDate = 0;
    size_t strDateLength;

    outputFile = fopen("Output.dat", "w");

    if(outputFile == NULL)
    {
        fprintf(stderr, "Couldn't write to the file.\n");
        return FALSE;
    }

    while(fgets(buffer, BUFFERSIZE, stdin))
    {
        strDateLength = strlen(buffer);

        if(buffer[strDateLength - 1] != '\n' && strDateLength <  BUFFERSIZE - 1)
        {
            appendNewLine(buffer, strDateLength);
        }

        isValidFormat = validateDateFormat(buffer, &month, &days, &year, strDateLength);

        if(isValidFormat)
        {
            isValidDate = validateDate(month, days, year);

            if(isValidDate)
            {
                fprintf(outputFile, "%s", buffer);
            }
        }
    }

    fclose(outputFile);
    return TRUE;
}

void appendNewLine(char *str, size_t strLength)
{
    str[strLength] = '\n';
    str[strLength + 1] = '\0';
}

int validateDateFormat(char *str, int *month, int *days, int *year, size_t strDateLength)
{
    int index, index2 = 0;
    char temp[BUFFERSIZE];
    int numOfForwSlashes = 0;

    for(index = 0; index < strDateLength; index++)
    {
        if(str[index] == '/')
        {
            if(numOfForwSlashes == 0)
            {
                *month = atoi(temp);
            }
            else if(numOfForwSlashes == 1)
            {
                *days = atoi(temp);
            }

            numOfForwSlashes++;
            memset(&temp[0], 0, sizeof(temp));
            index2 = 0;
        }
        else if(str[index] == '\n')
        {
            *year = atoi(temp);
        }
        else if(!isdigit(str[index]) && str[index] != ' ')
        {
            return FALSE;
        }
        else
        {
            temp[index2] = str[index];
            index2++;
        }
    }

    if(numOfForwSlashes != TOTALFORWARDSLASHES) // This define is 2
    {
        return FALSE;
    }
    else
        return TRUE;
}

int validateDate(int month, int days, int year)
{
    if((month < JANUARY || month > DECEMBER) && (days < 1 || days > THIRTYONE))
    {
        return FALSE;
    }

    if(month == FEBURARY)
    {
        if(isLeapYear(year))
        {
            if(days > TWENTYNINE)
            {
                return FALSE;
            }
        }
        else
        {
            if(days > TWENTYEIGHT)
            {
                return FALSE;
            }
        }
    }
    else if(month == APRIL || month == JUNE || month == SEPTEMBER || month == NOVEMBER)
    {
        if(days > THIRTY)
        {
            return FALSE;
        }
    }
    else
    {
        if(days > THIRTYONE)
        {
            return FALSE;
        }
    }

    return TRUE;
}

问题:

除了缺少年份的情况外,它在大多数情况下都有效,坦率地说,我想知道是否有办法在仍然遵循标准的同时使它更简洁。使用 sscanf 或 scanf 并不能解决我遇到的所有问题,strtok 之类的字符串函数也无济于事。

最佳答案

... it works except for cases where the year is missing and to be frank I would like to know if there is way I could make this more concise ...

与其只创建一个函数来解析日期,不如创建一个辅助函数来解析每个整数。

未经测试的代码示例:

#include <ctype.h>
#include <errno.h>
#include <stdlib.h>

// Parse 1 int and optional following delimiter
// Return NULL on error
static const char *Get_int(const char *p, int *i, int endchar) {
  if (p) {
    char *endptr;
    errno = 0;
    long y = strtol(p, &endptr, 10);
    if (errno || p == endptr) return NULL;
    *i = y;  // Test for INT_MIN... INT_MAX if desired

    // skip following white-space
    while (isspace((unsigned char ) *endptr))
      endptr++;

    // Look for delimiter
    if (endchar) {
      if (*endptr != endchar) return NULL;
      endptr++;
    }
    return endptr;
  }
  return NULL;
}

int validateDateFormat(char *str, int *month, int *days, int *year, 
    size_t strDateLength) {
  // form string
  char temp[strDateLength + 1];
  memcpy(temp, str, strDateLength);
  temp[strDateLength] = '\0';
  const char *p = temp;

  p = Get_int(p, month, '/');
  p = Get_int(p, days, '/');
  p = Get_int(p, year, 0);
  return p && *p == '\0';
}

sscanf() 方法:

使用 "%n" 检测是否扫描了整个字符串。

int validateDateFormat(char *str, int *month, int *days, int *year, 
    size_t strDateLength) {
  // form string
  char temp[strDateLength + 1];
  memcpy(temp, str, strDateLength);
  temp[strDateLength] = '\0';

  int n = 0;
  sscanf(temp, "%d /%d /%d %n", month, days, year, &n);
  return n > 0 && temp[n] == '\0';
)

黑客利用:请注意,下面的代码假定 strDateLength > 0fgets() 将很乐意将空字符作为第一个字符读入并继续读取,直到遇到 '\n'

由于 strDateLength 是一个 size_t,一个无符号类型,0-1 是一个巨大的值,而 buffer[strDateLength - 1 ]肯定会出问题。

while(fgets(buffer, BUFFERSIZE, stdin)) {
  strDateLength = strlen(buffer);
  if(buffer[strDateLength - 1] != ...

顺便说一句,无论如何都不需要添加 '\n'

关于c - 从重定向为输入的文件中验证日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36046040/

相关文章:

java - 如何避免 Hibernate Validator ConstraintDeclarationException?

c# - 值是逗号而不是点的范围验证

ios - 没有标题的字段的 XLForm formValidationErrors 显示 "(null)"

sql - Oracle SQL 按季度计算的美元总和

javascript - js基于字符串的日期对象

c - 用C语言设计主站(PC或ARM板)和从站(微处理器)之间简单而强大的串行协议(protocol)

c - 在 C 中提取 double 的最右边 N 位

c - 无法初始化 union 中的第二项

c - 用数组填充数组?

java - 如何更正 EST 格式的日期?