简体   繁体   English

以特定格式读取日期和时间

[英]Reading date and hour with an specific format

I have been working on a program that reads a string which is supossed to be the date and the time with the following format dd-yy-aaaa hh:mm:ss. 我一直在研究一个程序,该程序读取的字符串可能是日期和时间,其格式为dd-yy-aaaa hh:mm:ss。 It does it n times, and at the end it prints the number of strings that were entered in the correct format. 它执行n次,最后打印以正确格式输入的字符串数。 I haven't been able to make it only count the dates that have, for example, the year on a four digit format. 我一直无法使它仅以四位数字格式计算例如年份的日期。 It counts them as long as the data entered are numbers. 只要输入的数据是数字,它就会对它们进行计数。 How do i fix it? 我如何解决它? Here's my code 这是我的代码

#include <stdio.h>

int main(){
    char string[100];
    int day[10];
    int month[10];
    int year[10];
    int hour[10];
    int minute[10];
    int second[10];
    int n,i=0;
    scanf("%d",&n);
    fflush(stdin);
    while(n!=0){
    scanf("%[^\n]s", &string);
    fflush(stdin);
        if((sscanf(string,"%d%d-%d%d-%d%d%d%d %d%d:%d%d:%d%d",day,month,year,hour,minute,second))==6){
        i++;
    }
    n--;
    }
    printf("%d",i);

}

You will make your life a lot easier if you use strptime , ie: 如果使用strptime ,将使您的生活更加轻松,即:

From the manpage: 从联机帮助页:

NAME 名称

  strptime - convert a string representation of time to a time tm structure 

SYNOPSIS 概要

  #define _XOPEN_SOURCE /* See feature_test_macros(7) */ #include <time.h> char *strptime(const char *s, const char *format, struct tm *tm); 

DESCRIPTION 描述

The strptime() function is the converse function to strftime (3) and converts the character string pointed to by s to values which are stored in the tm structure pointed to by tm , using the format specified by format . strptime()函数是相反的功能,以strftime (3)并且将字符串指向s到被存储在TM结构指向的值tm ,使用由指定的格式format Here format is a character string that consists of field descriptors and text characters, reminiscent of scanf(3) . 这里的format是一个由字段描述符和文本字符组成的字符串,让人想起scanf(3) Each field descriptor consists of a % character followed by another character that specifies the replacement for the field descriptor. 每个字段描述符都由一个%字符组成,后跟另一个字符,用于指定该字段描述符的替换。 All other characters in the format string must have a matching character in the input string, except for whitespace, which matches zero or more whitespace characters in the input string. 格式字符串中的所有其他字符在输入字符串中必须具有匹配的字符,但空格除外,该空格与输入字符串中的零个或多个空格字符匹配。 There should be whitespace or other alphanumeric characters between any two field descriptors. 任何两个字段描述符之间都应该有空格或其他字母数字字符。

And so it continues ... 所以它继续...

A few notes to clarify: 一些注意事项以澄清:

  1. Why are day etc. arrays of length 10? 为什么day等长度为10的数组? Do you expect n <= 10 and to save each date? 您是否希望n <= 10并保存每个日期?

  2. You need a return statement. 您需要一个return声明。

  3. Is i supposed to be the number of successfully parsed dates? i应该是成功解析日期的数量吗?

Ok, so there are some problems with the way you're using sscanf (but yay for checking the return value!) - here's the specification . 好的,所以您使用sscanf的方式存在一些问题(但是要检查返回值!)-这是规范

  1. sscanf must have the address to which you want to read the input - so &day[i] presumably. sscanf必须具有您要读取输入内容的地址-大概是&day[i]

  2. %d will read the whole int , not just one digit - that is, %d将读取整个int ,而不仅仅是一位数字-也就是说,

      sscanf(string, "%d", &my_int_var); 

will read the first int it encounters, whether that be 1, 10, 1987, 2008875, etc. 将读取它遇到的第一个int ,无论是1、10、1987、2008875等。

eg to fix 例如修复

char string[100];
int day;
int month;
int year;
int hour;
int minute;
int second;
int n,i=0;
scanf("%d", &n);
fflush(stdin);//non-portable
while(n>0){
    scanf(" %99[^\n]", &string);
    fflush(stdin);
    if((sscanf(string,"%2d-%2d-%4d %2d:%2d:%2d", &day,&month,&year,&hour,&minute,&second))==6){
        //range check
        ++i;
    }
    --n;
}
printf("%d", i);

Use fgets() to read data and then parse using sscanf() features: 使用fgets()读取数据,然后使用sscanf()功能进行解析:
"%*1d%*1d" to detect 2 digits, "%*1d%*1d"可检测2位数字,
"%*1[ ]" to detect space "%*1[ ]"以检测空间
"%n" for the correct length and ending. "%n"表示正确的长度和结尾。

// 1 good, 0 bad
int CheckDate(const char *buffer) {
  static const char format[] = "dd-yy-aaaa hh:mm:ss\n";
  #define Space "%*1[ ]"
  #define Dig2 "%*1d%*1d"
  #define Dig4 Dig2 Dig2
  int n = 0;
  sscanf(buffer, Dig2 "-" Dig2 "-" Dig4 Space Dig2 ":" Dig2 ":" Dig2 "%n", &n);
  return (sizeof(format) - 2 == n) && (buffer[n] == '\n');
}

int main() {
  int i = 0;
  int n = 0;
  scanf("%d%*c", &n);
  while (n-- > 0) {
    char buffer[100];
    if (fgets(buffer, sizeof buffer, stdin) == NULL) break; // EOF
    i += CheckDate(buffer);
  }
  printf("%d",i);
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM