[英]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 tostrftime
(3) and converts the character string pointed to bys
to values which are stored in the tm structure pointed to bytm
, using the format specified byformat
.的
strptime()
函数是相反的功能,以strftime
(3)并且将字符串指向s
到被存储在TM结构指向的值tm
,使用由指定的格式format
。 Hereformat
is a character string that consists of field descriptors and text characters, reminiscent ofscanf(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: 一些注意事项以澄清:
Why are day
etc. arrays of length 10? 为什么
day
等长度为10的数组? Do you expect n <= 10
and to save each date? 您是否希望
n <= 10
并保存每个日期?
You need a return
statement. 您需要一个
return
声明。
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
的方式存在一些问题(但是要检查返回值!)-这是规范 。
sscanf
must have the address to which you want to read the input - so &day[i]
presumably. sscanf
必须具有您要读取输入内容的地址-大概是&day[i]
。
%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.