6

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?

5
  • 1
    Which string? btw LPRSTR, GPTR and CHAR are not standard c++ types. Commented Nov 2, 2010 at 13:11
  • I need to check if hwndTitleValue is empty. Commented Nov 2, 2010 at 13:13
  • GlobalAlloc returns a HANDLE, you cant convert it to a string like that Commented Nov 2, 2010 at 13:15
  • @ile, it would be proper etiquette to select the answer that solved your problem as the answer, and open a new question with your new problems. Commented Nov 2, 2010 at 13:48
  • 2
    @ile: Your second question is unrelated. Please start a new question. Commented Nov 2, 2010 at 14:57

7 Answers 7

18

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the right answer. Forget about strlen(). strlen() will scan the whole string that can be very long. If the string is a gazillion characters long it will scan them all. Only check the first character to detect an empty string.
this works nice. I edited my question and now I need also to check if the file exists. Could you please check for the file? Thank you very much!
Checking for file existence is a different problem altogether. Best to ask that question separately - or better yet, search StackOverflow. That question has likely been asked before.
11

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

}

9 Comments

If string is NULL, this function doesn't return anything.
but I don't see connection between your code and hwndTitleValue that I use. Thank you!
One more revision, where you got rid of the if statement and simply returned string != NULL && string[0] = 0 and the answer would be perfect:)
@ile: call isEmpty(hwndTitleValue) and it will give you 1 for empty strings and 0 for non-empty strings.
The OP asks for a C++ solution: why did you use a int instead of a bool for the return value type ?
|
3

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.

1 Comment

Shouldn't any character array starting with then null terminator be considered empty, regardless of length?
2

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.

4 Comments

True, but doesn't answer the question.
@Graeme Perrow: Why will not a check if( *str == 0 ) be an answer?
Because that part of the answer wasn't there when I added my comment.
@Graeme Perrow: Mmkay, it is there now.
2

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

1 Comment

This is horrible. This will scan the entire string that can potentially be veeeeery loong.
0

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.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.