Share via


how to check for null/empty string value of LPCTSTR

Question

Thursday, March 29, 2007 5:43 AM

hello

i have these sample codes which has some problems:

(LPCTSTR sAppName, LPCTSTR sServer, int nPort, CString sFile)

if ((sAppName == L"") || (sServer == L"") || (nPort == 0) || (sFile == L""))
    {
        return 0;
    }

When I pass an empty string for sFile, it will return 0;
But when I pass an empty string for sAppName and sServer, it will not return 0 but instead it will continue with the rest of the codes below.

How to check for null or empty string value for LPCTSTR?

thanks

All replies (9)

Thursday, March 29, 2007 6:46 AM âś…Answered

if ((sAppName == _T('\0')) || (sServer == _T('\0')) ||
        (nPort == 0) || (sFile == L""))
    {
        return 0;
    }

Modify your code to,

if ((wcslen(sAppName) == 0 ) || (

wcslen(sServer) == 0 ) ||
        (nPort == 0) || (sFile.IsEmpty() == TRUE))
    {
        return 0;
    }

 

Make sure sAppName, and  sServer

 are pointing to empty string.


Thursday, March 29, 2007 5:56 AM

 kaye wrote:

[...]

How to check for null or empty string value for LPCTSTR?

 

I hope this works:

 

Code Snippet

if( sAppName == NULL || *sAppName == _T('\0') || . . .

 

 


Thursday, March 29, 2007 6:09 AM

thank you very much

i modified the codes into this:

if ((sAppName == _T('\0')) || (sServer == _T('\0')) ||
        (nPort == 0) || (sFile == L""))
    {
        return 0;
    }

but still it will not get inside the return 0 statement when I pass an empty string.

i gave values this way:

dwStatusActual = pNS->GetWebFile(L"", sFixedServerName, nFixedPort,
                sFixedFileName);

wherein the first parameter is an empty string.
so it should return 0 when the if statement is processed.

thanks


Thursday, March 29, 2007 6:38 AM

 kaye wrote:
thank you very much

i modified the codes into this:

if ((sAppName == _T('\0')) || (sServer == _T('\0')) ||
        (nPort == 0) || (sFile == L""))
    {
        return 0;
    }

but still it will not get inside the return 0 statement when I pass an empty string.

[...]

 

I think you should further modify it into this:

 

Code Snippet

if( *sAppName == _T('\0') || *sServer == _T('\0') || nPort == 0 || sFile.IsEmpty())

{

    return 0;

}

 

I hope it works now.

 

 


Thursday, March 29, 2007 6:41 AM

What you do is not correct. You compare a const pointer to char* with a character. That is not going to work. You should follow Viorel's indications. If you want to compare a LPCTSTR with a string literal, you should use strcmp(), or macro _tcscmp().

 

Code Snippet

if(_tcscmp(sAppName, _T("something")) == 0)
  ...

 

 


Thursday, March 29, 2007 7:04 AM

i changed my codes to this:

if ((*sAppName == _T('\0')) || (*sServer == _T('\0')) ||
        (nPort == 0) || (sFile == L""))
    {
        return 0;
    }

and it worked fine....

i also used this kind of code by **Prasad Somwanshi
**and it also worked.

if ((wcslen(sAppName) == 0 ) || (wcslen(sServer) == 0 ) ||
        (nPort == 0) || (sFile.IsEmpty() == TRUE))
    {
        return 0;
    }

thanks to all.


Thursday, March 29, 2007 7:14 AM

Hi,

 

Can the following code be considered as valid answer for the above question?

 


bool IsEmptyString(LPCTSTR szText)
{
return (NULL == szText || (CString(szText)).TrimLeft().TrimRight().GetLength() ==0);
}  
//and
  LPCTSTR sAppName = _T("AppName");
LPCTSTR sServer = _T("sever");
LPCTSTR sFile = _T("");
int nPort = 1;
if (IsEmptyString(sAppName)|| IsEmptyString(sServer) || (nPort == 0) || IsEmptyString(sFile))
{
return 0;
}

 

 

Thanx,

Ch.T.Gopi Kumar.


Thursday, March 29, 2007 3:52 PM

If whitespaces are considered to contribute to "emptyness" then that implementation of IsEmptyString() is correct. But I wouldn't introduce the MFC dependency if there wasn't already one there.

 

I would have answered the original post by suggesting two inlined versions to be placed in a shared header:

Code Snippet

inline bool IsNullOrEmpty( const char* str )
{
   return (str == 0) || (*str == '\0');
}

inline bool IsNullOrEmpty( const wchar_t* str )
{
   return (str == 0) || (*str == L'\0');
}

 
For whitespace trimming, I would provide separate functions.  This is best done by providing a function that returns std :: string.  Use string :: empty().  Note that the string( const char* ) constructor must take a non-null value.

e.g.
assert( szText != 0 );  // we should always have a string
string trimmedStr = TrimString( szText );

e.g.
if( (szText != 0) || TrimString(szText).empty() ) ...

In general, its better practice to always have non-null strings, and limit your use of IsNullOrEmpty().  Keep your local state constrained to values that you can easily deal with, rather than propating the null-case all over your code. 

And finally, start getting in the habit of using std :: string more than C-strings, less you really have a performance requirement (and there usually isn't one when it comes to strings). 

 

Brian


Thursday, March 29, 2007 4:03 PM

Thanks Brain for good explanation and the suggestions.

 

Best Regards,

Ch.T.Gopi Kumar.