Verify that the string is empty.

I have a C ++ project that I need to edit. This is a variable declaration:

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

How to check if this line is empty?

I tried just with if(hwndTitleValue == "") , but always returns false. How to check if this line is empty?

EDIT

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

  // 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 empty?

+6
c ++ string
source share
7 answers

I believe hwndTitleValue is a pointer, at least in Hungarian notation. Your method allocates an array of bytes (ANSI C string), so the best way to do this is

 #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); } 

It should be noted that the line may be empty, but filled with a space. This method will tell you that the string contains data (spaces are data!). If you need to account for lines filled with spaces, you need to trim them first.


EDIT . Another thing to note is that the above methods do not take into account valid NULL pointers. If the pointer is null, isEmpty will return FALSE, which is undesirable. We can remove the NULL check, and then it becomes the responsibility of the caller, or we can determine that isEmpty returns FALSE to NULL.

 #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, ""); } 
+11
source share

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 the function call.

+18
source share

First of all, this is not a string. Not yet. It’s just a pointer to a piece of memory, which in every sense and purpose contains garbage, that is, some random data.

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

+3
source share

GlobalAlloc() will return a memory block filled with zeros (thanks to the GPTR flags), and not a string. There is no point in checking. You better check that the returned pointer is not null.

If this is not enough for you, just check

 if (*hwndTitleValve == 0 ) { } 

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

+2
source share

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

 if (strlen(hwndTitleValve) == 0) 

assuming an ANSI string. Note that this will be better labeled as β€œC” and β€œWindows”, not C ++.

+2
source share

It seems strange to me that the line name starts with hwnd (for Windows handles), but in any case, you can assume that LPSTR is the same as char *, and just use something like strlen to check its length .

0
source share

if you want to check if the memory allocation failed, do this as follows:

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

if (hwndTitleValue == NULL) return ALLOC_FAILED;

I see no reason to work with strings here.

-one
source share

All Articles