473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating a string with sscanf

I'm validating a date and time string which must be EXACTLY of the format

yy-mm-dd hh:mm:ss

and extracting the six numeric values using sscanf.

I'm using the format string "%2u-%2u-%2u %2u:%2u:%2u" which works, but it
allows each numeric field to be either 1 or 2 digits in length. Also the man
page for sscanf says that preceeding white space before a numeric is
ignored.

But I need the input string to be EXACTLY of the above format (each numeric
is exactly 2 digits long and there is no white space) otherwise the input
string should be rejected.

What is the best way of validating the input string against an EXACT format
string and exctracting the six numeric values, or must I manually examine
every character of the input string?

Jun 27 '08 #1
6 4491
Richard wrote:
I'm validating a date and time string which must be EXACTLY of the format

yy-mm-dd hh:mm:ss
[snip]
What is the best way of validating the input string against an EXACT
format string and exctracting the six numeric values, or must I manually
examine every character of the input string?
If you want to know the absolute best way, then you have one of three
courses of action:

1. Use a DSL that is suited to this task and that generates C code.
lex/flex is quite good.

2. Use a library that provides regexes, and validate with that. Regex
libraries are a dime a dozen, too. For example, they are in POSIX.2
(see 'man 3 regex' on Unix).

3. Drop C and use a language that has (1) or (2) built-in, like Perl or Awk
or what have you.
These strategies have disadvantages, though. (1) means you need to learn a
new language, (2) means you now have two problems ;-), and (3) might not be
an option depending on your situation. At this point you'll have to look
into finite state automata.

Jun 27 '08 #2
"Richard" <nu**@null.co.u kwrites:
I'm validating a date and time string which must be EXACTLY of the format

yy-mm-dd hh:mm:ss

and extracting the six numeric values using sscanf.

I'm using the format string "%2u-%2u-%2u %2u:%2u:%2u" which works, but
it allows each numeric field to be either 1 or 2 digits in
length. Also the man page for sscanf says that preceeding white space
before a numeric is ignored.

But I need the input string to be EXACTLY of the above format (each
numeric is exactly 2 digits long and there is no white space)
otherwise the input string should be rejected.

What is the best way of validating the input string against an EXACT
format string and exctracting the six numeric values, or must I
manually examine every character of the input string?
If all you want (at this stage) is syntactic correctness, then you
could test that sscanf returns 6, strlen(data) == 17 and that data
contains only one character for which isspace is true.

--
Ben.
Jun 27 '08 #3

"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
"Richard" <nu**@null.co.u kwrites:
>I'm validating a date and time string which must be EXACTLY of the format

yy-mm-dd hh:mm:ss

and extracting the six numeric values using sscanf.

I'm using the format string "%2u-%2u-%2u %2u:%2u:%2u" which works, but
it allows each numeric field to be either 1 or 2 digits in
length. Also the man page for sscanf says that preceeding white space
before a numeric is ignored.

But I need the input string to be EXACTLY of the above format (each
numeric is exactly 2 digits long and there is no white space)
otherwise the input string should be rejected.

What is the best way of validating the input string against an EXACT
format string and exctracting the six numeric values, or must I
manually examine every character of the input string?

If all you want (at this stage) is syntactic correctness, then you
could test that sscanf returns 6, strlen(data) == 17 and that data
contains only one character for which isspace is true.
Thanks. As this is a one-off, I've ended up checking all 17 characters
individually, with isdigit being used for each character position where a
numeric is required, and then calling sscanf to decode it.

Jun 27 '08 #4
Richard wrote:
>
I'm validating a date and time string which must be EXACTLY of
the format
yy-mm-dd hh:mm:ss
and extracting the six numeric values using sscanf.
I think you should make great efforts to use ISO approved date
forms, which means that the year field will contain 4 digits. This
will avoid any further year 2000 flaps until 10000 AD, at which
point neither of us will probably care.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #5
On Sun, 27 Apr 2008 13:14:21 -0400, CBFalconer <cb********@yah oo.com>
wrote in comp.lang.c:
Richard wrote:

I'm validating a date and time string which must be EXACTLY of
the format
yy-mm-dd hh:mm:ss
and extracting the six numeric values using sscanf.

I think you should make great efforts to use ISO approved date
forms, which means that the year field will contain 4 digits. This
will avoid any further year 2000 flaps until 10000 AD, at which
point neither of us will probably care.
Speak for yourself, I plan on being around that long.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #6
Richard wrote:
I'm validating a date and time string which must be EXACTLY
of the format

yy-mm-dd hh:mm:ss

and extracting the six numeric values using sscanf.

I'm using the format string "%2u-%2u-%2u %2u:%2u:%2u"
which works, but it allows each numeric field to be either 1 or
2 digits in length. Also the man page for sscanf says that
preceeding white space before a numeric is ignored.

But I need the input string to be EXACTLY of the above
format (each numeric is exactly 2 digits long and there
is no white space) otherwise the input string should be
rejected.

What is the best way of validating the input string against
an EXACT format string and exctracting the six numeric
values, or must I manually examine every character of
the input string?
Pretty much, though you can do it with scanf...

#include <stdio.h>

#define DIGIT "%1[0123456789]"
#define SPACE "%*1[ ]"

#define FORMAT \
DIGIT DIGIT "-" DIGIT DIGIT "-" DIGIT DIGIT \
SPACE \
DIGIT DIGIT ":" DIGIT DIGIT ":" DIGIT DIGIT

#define DIGIT_PAIR_TO_I NT(X) \
( (X ## 1[0] - '0') * 10 + (X ## 2[0] - '0') )

int main(int argc, char **argv)
{
if (argc)
while (argv++, --argc)
{
char Y1[2], Y2[2];
char M1[2], M2[2];
char D1[2], D2[2];
char h1[2], h2[2];
char m1[2], m2[2];
char s1[2], s2[2];

int r = sscanf( *argv,
FORMAT,
Y1, Y2, M1, M2, D1, D2,
h1, h2, m1, m2, s1, s2 );

if (r == 12)
{
int Y = DIGIT_PAIR_TO_I NT(Y);
int M = DIGIT_PAIR_TO_I NT(M);
int D = DIGIT_PAIR_TO_I NT(D);
int h = DIGIT_PAIR_TO_I NT(h);
int m = DIGIT_PAIR_TO_I NT(m);
int s = DIGIT_PAIR_TO_I NT(s);

printf(" match:");
printf(" %02d-%02d-%02d", Y, M, D);
printf(" %02d:%02d:%02d" , h, m, s);
printf("\n");
}
else
printf("no match: %s\n", *argv);
}

return 0;
}

--
Peter

Jun 27 '08 #7

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

Similar topics

6
18486
by: Allan Bruce | last post by:
I have a string like: "FL:1234ABCD:3:FileName With Spaces.txt\n" and I want to read the values separated by ':' into variables. I tried to use sscanf like this: sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName); but the lGUID just continues until whitespace or \0 is found. I need a way
15
20811
by: qazmlp | last post by:
What is the best & fastest way of validating an IPv4 address? Basically, the input can be either in IPAddressv4 or IPAddressv4:port format. Currently I have the following code to validate the first format. Does anybody have any comment on this? Also, please suggest a mechanism to validate the second format(address:port) also. Thanks!
16
5966
by: Christopher Benson-Manica | last post by:
I'm wondering about the best way to do the following: I have a string delimited by semicolons. The items delimited may be in any of the following formats: 1) 14 alphanum characters 2) 5 alphanums space 8 alphanums 3) 6 alphanums colon 8 alphanums 4) 5 alphanums colon 8 alphanums My task is to convert items in the third format to the...
7
1453
by: millerm | last post by:
I'm obviously new to C, and have been trying different things to get this done, but I'm at the end of the line and need some suggestions. I am reading a string in from a user, in the form of a name, score, and number of questions. It will be something like: Mark 98.2 20 all separated by spaces. I want to move each element into a temporary...
2
5194
by: bildad | last post by:
The following 'book example' of validating input seems to be incomplete. Since it is a beginner's book it may be intentional for simplicity. But I would like to know how to make this program work for all invalid input. Just for example, if user inputs 'abc' an error is caught, but if user inputs '432' the program hangs. Any clarification...
6
4194
by: Markus Ilmola | last post by:
How to a parse a string using C++ (standard library) same way as sscanf in C. For example if a have a string: My name is "John Smith" and I'm 13 years old and 120 cm tall. and a want to parse the name (string that can be empty (whitout the quotation marks)), age (unsigned int) and height (unsigned int).
9
5828
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. The user will be presented with a list of 5 selections they can make. They will then be prompted for which selection they want to enter (which can...
232
13096
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an...
9
6635
by: Bint | last post by:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc i can use sscanf(string,"success=%d", &d) to get the success value. but after that i just want to read name and u pairs until there are no more. if Iwere to do a sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of the first u/name, right? is...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.