简体   繁体   中英

Check if string is empty

I got a project in C++ which I need to edit. This is a declaration of variable:

LPSTR hwndTitleValue = (LPSTR)GlobalAlloc(GPTR,(sizeof(CHAR) * hwndTitleSize));

How to check if this string is empty?

I tried simply with if(hwndTitleValue == "") but it always returns false. How to check if this string is empty?

EDIT

I also need to check if the file is attached. Here is the code of the file:

    // Attachment
    OFSTRUCT ofstruct;
    HFILE hFile = OpenFile( mmsHandle->hTemporalFileName , &ofstruct , OF_READ );
    DWORD hFileSize = GetFileSize( (HANDLE) hFile , NULL );
    LPSTR hFileBuffer = (LPSTR)GlobalAlloc(GPTR, sizeof(CHAR) * hFileSize );
    DWORD hFileSizeReaded = 0;
    ReadFile( (HANDLE) hFile , hFileBuffer, hFileSize, &hFileSizeReaded, NULL );
    CloseHandle( (HANDLE) hFile );

How to check if hFile is empty?

The easiest way to check if a string is empty is to see if the first character is a null byte:

if( hwndTitleValue != NULL && hwndTitleValue[0] == '\0' ) {
    // empty
}

You can use strlen or strcmp as in other answers, but this saves a function call.

I believe that hwndTitleValue is a pointer, at least in Hungarian Notation it would be. Your method is allocating a array of bytes (an ANSI C string), so the best way to do it would be

#include <string.h>
// ... other includes ...

int isEmpty(LPSTR string)
{
    if (string != NULL)
    {
        // Use not on the result below because it returns 0 when the strings are equal,
        // and we want TRUE (1).
        return !strcmp(string, "");
    }

    return FALSE;
}

You can, however, hack the above method and not use strcmp:

#include <string.h>
// ... other includes ...

int isEmpty(LPSTR string)
{
    // Using the tip from Maciej Hehl
    return (string != NULL && string[0] == 0);
}

One thing to note is that the string might not be empty but filled with space. This method will tell you that the string has data (spaces are data!). If you need to account for strings filled with spaces, you will need to trim it first.


EDIT : Another thing to note is that the methods above do not account from NULL pointers correctly. If the pointer is null, isEmpty will return FALSE, which isn't desired. We can remove the NULL check and then it becomes responsibility of the caller, or we can define that isEmpty returns FALSE to NULL strings.

#include <string.h>
// ... other includes ...

int isEmpty(LPSTR string)
{
    // Always return FALSE to NULL pointers.
    if (string == NULL) return FALSE;

    // Use not on the result below because it returns 0 when the strings are equal,
    // and we want TRUE (1).
    return !strcmp(string, "");

}

First of all, this is not a string. Not yet. It's just a pointer to a chunk of memory that for all intents and purposes contains garbage, ie some random data.

Strings in C are pointers to zero-terminated character arrays. So your empty string "" is actually an array of one element with value zero. But your comparison is between pointers , so it always fails.

GlobalAlloc() will return a memory block filled with zeroes (thanks to GPTR flag), not a string. There's no point in checking. You'd better checked that the pointer returned is not null.

If that is not enough for you an just check

if (*hwndTitleValve == 0 ) {
}

A valid empty string will store a null terminator at the very beginning.

The GlobalAlloc function just allocates and returns a block of memory and the GPTR option zeroes the bytes of the allocated memory so you can just use:

if (strlen(hwndTitleValve) == 0)

assuming an ANSI string. Note that this would be better tagged as "C" and "Windows" rather than C++.

我觉得奇怪的是字符串名称以hwnd开头(这是用于windows句柄),但无论如何你可以假设LPSTR与char *相同,只是使用类似strlen的东西来检查它的长度。

if you want to check whether memory allocation failed do it this way:

HGLOBAL hwndTitleValue = GlobalAlloc(GPTR,(sizeof(CHAR) * hwndTitleSize));

if (hwndTitleValue == NULL) return ALLOC_FAILED;

I see no point working with strings here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM