473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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?
Jun 28 '08 #1
5 7955
Timo wrote:
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;
}
Jun 28 '08 #2
Timo wrote:
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
Jun 28 '08 #3
On 28 Haziran, 15:31, Timo <t_i_m_0_n...@yahoo.comwrote:
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);
Jun 28 '08 #4
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.

Jun 28 '08 #5
Timo wrote:
>
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.

Jun 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brent Lievers | last post by:
Greetings, I have a question about parsing a fixed-width integer from a string. Lines of data are being read from a file having a very strict column-delimited format. In my example below,...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
6
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the...
7
by: RSoIsCaIrLiIoA | last post by:
until a poor newbie can build a better function than sscanf and fgets scanf("%s", string) is like gets(string)
19
by: mathieu | last post by:
Hello, I am trying to parse a string and I am surprised by the result(*) When using: sscanf(s, "%03u%c", &val, &f); If a number less than 3 digits is found, shouldn't sscanf report that ? ...
8
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a...
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.