读取与sscanf的日期 [英] reading a date with sscanf
问题描述
我有阅读的日期格式(DD-MM-YYYY)使用sscanf的这条线code从一个char *字符串:
I've got to read a date format (dd-mm-yyyy;) from a char* string using sscanf with this line of code:
sscanf(string, "%[^-]-%[^-]-%[^;];", day, month, year)
在哪里日/月/年的焦炭*的3/3/5 elemets每串
where day/month/year are char* string of 3/3/5 elemets each.
在这种方式,它的工作原理,但C>),我得到了一个 0 0 0
值当我使用的printf
在他们身上。如果我改变的sscanf
到
In this way it works but when i try to use day/moth/year as integer
variables (with same sscanf
), I got a 0 0 0
value when i use printf
on them. If i change sscanf
to
sscanf(string, "%d-%d-%d;", day, month, year)
我分段故障
。
任何人都可以帮我吗?使用K&放大器;的。R本书中,我没有找到一个解决方案。
Anyone can help me? Using K&R book, I didn't find a solution.
推荐答案
您需要通过 INT
变量的地址:
You need to pass the address of the int
variables:
int day, month, year;
sscanf(string, "%d-%d-%d;", &day, &month, &year);
的sscanf()
返回分配取得的数量,建议使用之前检查返回值天
,月
和年
:
sscanf()
returns the number of assignments made, recommend checking the return value before using day
, month
and year
:
if (3 == sscanf(string, "%d-%d-%d;", &day, &month, &year))
{
}
这篇关于读取与sscanf的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!