|  | Simple sscanf parsing problem |  | |
| | | Timo |  |
| Posted: Sat Jun 28, 2008 12:31 pm Post subject: Simple sscanf parsing problem |  |
I haven't been using ANSI-C for string parsing for some time, so even this simple task is problematic:
I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49".
I try to parse date from this string to variables tmp1, tmp2, tmp3:
1st attempt: sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3);
All variables get value 0.
2nd attempt sscanf(tmp_str, "%d %d %d", &tmp1, &tmp2, &tmp3);
tmp3 = 25, others 0.
Any suggestions? |
| |
| | | pete |  |
| Posted: Sat Jun 28, 2008 12:31 pm Post subject: Re: Simple sscanf parsing problem |  |
Timo wrote:
| Quote: | I haven't been using ANSI-C for string parsing for some time, so even this simple task is problematic:
I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49".
I try to parse date from this string to variables tmp1, tmp2, tmp3:
1st attempt: sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3);
All variables get value 0.
|
I am unable to duplicate your problem.
/* BEGIN new.c output */
25 6 2008
/* END new.c output */
/* BEGIN new.c */
#include <stdio.h>
int main(void) { char *tmp_str = "25.6.2008 21:49"; int tmp1, tmp2, tmp3;
sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3); puts("/* BEGIN new.c output */\n"); printf("%d %d %d\n", tmp1, tmp2, tmp3); puts("\n/* END new.c output */"); return 0; }
/* END new.c */
-- pete |
| |
| | | CBFalconer |  |
| Posted: Sat Jun 28, 2008 12:31 pm Post subject: Re: Simple sscanf parsing problem |  |
Timo wrote:
| Quote: | I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49".
I try to parse date from this string to variables tmp1, tmp2, tmp3:
1st attempt: sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3);
All variables get value 0.
2nd attempt sscanf(tmp_str, "%d %d %d", &tmp1, &tmp2, &tmp3);
tmp3 = 25, others 0.
Any suggestions?
|
Never use a scanf function without checking the error return.
-- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. |
| |
| | | Guest |  |
| Posted: Sat Jun 28, 2008 12:46 pm Post subject: Re: Simple sscanf parsing problem |  |
Timo wrote:
Post a compilable complete code that exhibits the behaviour.
The following program outputs "25 6 2008 21 49".
=========================
#include <stdio.h>
int main(void) { int tmp1, tmp2, tmp3, tmp4, tmp5; const char date[] = "25.6.2008 21:49";
sscanf(date, "%d.%d.%d %d:%d", &tmp1, &tmp2, &tmp3, &tmp4, &tmp5); printf("%d %d %d %d %d\n", tmp1, tmp2, tmp3, tmp4, tmp5); return 0; } |
| |
| | | Ali Karaali |  |
| Posted: Sat Jun 28, 2008 1:00 pm Post subject: Re: Simple sscanf parsing problem |  |
On 28 Haziran, 15:31, Timo <t_i_m_0_n...@yahoo.com> wrote:
| Quote: | I haven't been using ANSI-C for string parsing for some time, so even this simple task is problematic:
I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49".
I try to parse date from this string to variables tmp1, tmp2, tmp3:
1st attempt: sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3);
All variables get value 0.
2nd attempt sscanf(tmp_str, "%d %d %d", &tmp1, &tmp2, &tmp3);
tmp3 = 25, others 0.
Any suggestions?
|
You can use sscanf (str, "%d%*c%d%*c%d %d%*c%d", &a, &b, &c, &d, &e); |
| |
| | | Timo |  |
| Posted: Sat Jun 28, 2008 1:08 pm Post subject: Re: Simple sscanf parsing problem |  |
Thanks for your replies. I found that the reason for this problem was because of variable types:
unsigned short tmp1; unsigned char tmp2, tmp3;
They should have been int's, or sscanf format parameters should have been modified. |
| |
|
|