?
Solved

How do I compare a variable of type LPTSTR against a String (returned by localtime)?

Posted on 2003-03-11
6
Medium Priority
?
1,637 Views
Last Modified: 2007-12-19
This is what I have:
int x       = _tcslen(NNew->Lst);
int y       = x - 1;
int z       = 1;
    for (LPTSTR R = &NNew->Lst[y]; (y >= 0) && (z <= 10); R--, y--, z++)
        if (! IsChNumeric(*R))
            break;
    *++R        = 0;
    y++;
TCHAR       szNum[_MAX_PATH];
    wsprintf(szNum, _T("%050lu"), NNew->GCounter);
    wsprintf(NNew->Nme, _T("%s%s"), NNew->Lst, &szNum[50 - (x - y)]);

I need to validate the first six digits of whatever is returned on this last line against the current date (returned by localtime). What is the easiest way to do this?



0
Comment
Question by:betsy05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 8112834
The type LPTSTR depends on your project settings.

IF you do not use unicode then LPTSTR is a regular string and so you can compare it with a string using regular strcmp:

void do_something(LPTSTR x, const char * y)
{
   if (strcmp(x,y) == 0) { // they're equal.
   }
}

If you use unicode then LPTSTR is a wchar_t string and you need to convert your const char * string to wchar_t before you can compare them. When both are wchar_t you use wcscmp() to compare the strings.

What function you use to convert the const char * string to wchar_t depends on what kind of data is in the const char * string and what kind of data in the wchar_t string. Normally the LPTSTR string is used to store unicode data and if that is the case then you simply need to convert the const char * string to unicode. This is almost a piece of cake.

If the string is only pure ascii characters (all characters are in the range 0..127 and you don't use any of the extended characters such as ÆØÅæøåäÄàÀ etc then it is very simple:

wchar_t * ascii2unicode(wchar_t * dst, const char * src)
{
   wchar_t * d = dst;
   while ((*d++ = *src++) != 0);
   return dst;
}

This function works like strcpy in that it just assume the space is enough, so you must make sure that the wchar_t buffer has room for as many characters as there are in the string src + 1 (the L'\0' char).

If you do use any of those characters from the extended set (ANSI characters above 127) then you need to be more cautious since Unicode maps them to mostly the same position as they are but I believe there are exceptions, not 100% sure about that though, in any case you can get far by simply:

wchar_t * ascii2unicode(wchar_t * dst, const char * src)
{
   wchar_t * d = dst;
   while ((*d++ = (unsigned char)*src++) != 0);
   return dst;
}

In any case it is also quite possible that the windows function mbstowcs() will do all this for you and also take care of all possible codes etc.

You could check up mbstowcs and see if that function does what you want it to do.

Alf
0
 
LVL 86

Expert Comment

by:jkr
ID: 8112844
Use '_tcscmp()' to do the comparation - it will use either UNICODE or ANSI, depending on your project settings.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8112926
jkr,

problem is that the other string to compare is const char string and not const TCHAR string.

betsy05 need a function:

int cmp(const TCHAR a[], const char b[])
{
   ....
}

function, the tcscmp() will only work if TCHAR happened to be char in which case it is the same as strcmp().

If TCHAR is wchar_t tcscmp() cannot be used - as far as I know.

In that case the mbstowcs() function can convert the const char string to const wchar_t string and wcscmp() can be used:

int cmp(const wchar_t a[], const char b[])
{
   int n = mbstowcs(NULL,b,0);
   wchar_t * p = new wchar_t[n + 1];
   mbstowcs(p,b,n+1);
   int res = wcscmp(a,p);
   delete [] p;
   return res;
}

Should do the compare if TCHAR is wchar_t. If TCHAR is char a regular strcmp() is enough.

Alf
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:jkr
ID: 8112981
>>problem is that the other string to compare is const char string and not const TCHAR string

Ooops - too busy working here...
0
 

Author Comment

by:betsy05
ID: 8121191
Thanks Salte.
LPTSTR Nme is 50 characters long. I want to get the first 6 characters only (mmddyy) and then compare the last two characters (year) against tm_year (returned by localtime), if they are different, error out. If year is the same, then compare the first two characters against tm_mon, if greater than tm_mon, error out, and then check the two characters in the middle (dd) against tm_mday. I need to prevent users from entering future dates. I was thinking I could use something like the GregorianCalendar class in Java. Any ideas?
0
 
LVL 12

Accepted Solution

by:
Salte earned 560 total points
ID: 8122537
GregorianCalendar class isn't available in C++ but there are equivalents.

convert the string to a tm_struct and then convert that tm_struct to a time_t value would be the best way to go I guess.

If you try to compare the last two digits of year make sure you don't fall into the trap that you compare those two digits with tm_year.

tm_year is year - 1900, so in 2003 the value should be 103, not 3, so if you compare with the last two digits of year you're way off.

Better to compare the full four digits of year with tm_year + 1900. That is safer comparison.

Alf
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question