Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C

Simple sscanf parsing problem

 
Jump to:  
 
Timo
PostPosted: 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
PostPosted: 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
PostPosted: 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
PostPosted: Sat Jun 28, 2008 12:46 pm    Post subject: Re: Simple sscanf parsing problem
       
Timo wrote:
Quote:
Any suggestions?

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
PostPosted: 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
PostPosted: 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.
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

grecja Shazza moja miłość - Big Cyc rodos last minute Moje jedyne marzenie - Anna Jantar nowe samochody