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? 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.
"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.
"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.
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 **
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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!
|
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...
|
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...
|
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...
| |
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).
|
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...
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |