ilija veselica
ilija veselica

Reputation: 9574

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?

Upvotes: 6

Views: 35610

Answers (7)

Bruno Brant
Bruno Brant

Reputation: 8564

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, "");

}

Upvotes: 11

Kra
Kra

Reputation: 678

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.

Upvotes: -1

Graeme Perrow
Graeme Perrow

Reputation: 57248

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.

Upvotes: 18

Nemanja Trifunovic
Nemanja Trifunovic

Reputation: 24561

I find it strange that a string name starts with hwnd (that's for windows handles), but anyway you can assume that LPSTR is the same thing as char* and just use something like strlen to check its length.

Upvotes: 0

uesp
uesp

Reputation: 6204

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++.

Upvotes: 2

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84169

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, i.e. 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.

Upvotes: 3

sharptooth
sharptooth

Reputation: 170499

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.

Upvotes: 2

Related Questions