[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

Extracting null-terminated strings

Is there an easy method of extracting a series of null-terminated strings from an array of characters, i.e. I have a char array with this in it...

string1\0string2\0string3\0\0


'\0' characters separate each string and the entire thing is terminated by two '\0' characters.

I need to be able to extract each of the strings in this. The only way I can think of is to iterate character by character... yuk.

This is under Windows btw, so any Windows API function that helps is ok.
0
paulburns
Asked:
paulburns
1 Solution
 
djbusychildCommented:
0
 
paulburnsAuthor Commented:
How exactly? I tried this code but it exits the loop after the first string found...

(szValue has the null-terminated strings in it)


LPTSTR lpszTok = _tcstok(szValue, _T("\0"));
while (lpszTok != NULL)
{
  strPrinter = lpszTok;
  lpszTok = _tcstok(NULL, _T("\0"));
}



0
 
ZoppoCommented:
Hi,

I don't think strtok is useful here coz strtok stops processing when the first \0 is reached.

you could do simly like this:

char* pCurrent = szValue;

while ( pCurrent != NULL )
{
 // do what you need to do with the strings here

 pCurrent += strlen( pCurrent ) + 1;

 if ( *pCurrent == 0 )
  pCurrent = NULL;
}


hope that helps,

ZOPPO
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AxterCommented:
Try the following:

int main(int argc, char* argv[])
{
char teststring[] = "string1\0string2\0string3\0\0string4\0string5\0string6 last\0";
unsigned SizeOfString = sizeof(teststring)-1;
while (strlen(teststring) < SizeOfString)
{
     teststring[strlen(teststring)] = ' ';
}
printf(teststring);
return 0;
}

0
 
ZoppoCommented:
But take care, coz this only works for strings instantiated as
char xy[] = ...
coz for i.e.
char *xy = ...
the sizeof() operator returns the size of the char* pointer, not the length of the string.

ZOPPO
0
 
AxterCommented:
I forgot about the double zero's.
Here's a better example:

int main(int argc, char* argv[])
{
     char teststring[] = "string1\0string2\0string3\0string4\0string5\0string6 last\0\0";
     unsigned SizeOfString = sizeof(teststring)-1;
     while (strlen(teststring) < SizeOfString)
     {
          if ((strlen(teststring)+1 < SizeOfString) && 
               !teststring[strlen(teststring)] &&
               !teststring[strlen(teststring)+1]) break;
          teststring[strlen(teststring)] = ' ';
     }
     printf(teststring);
     return 0;
}
0
 
AxterCommented:
>>the sizeof() operator returns the size of the char*
>>pointer, not the length of the string.
That's right.  That's why I didn't put sizeof() in the loop.
You have to have a method to dermine the max size of the buffer.  You don't need to know where "\0\0" is at, but you do need to know the maximum size of the buffer.
0
 
jasonclarkeCommented:
A C++ version using std::string:

std::vector<std::string> splitStrings(const char* s,size_t length)
{
    std::string text(s,length);

    std::vector<std::string> strings;
    size_t first = 0;
    while (first < text.size())
    {
        size_t last  = text.find('\0',first);
        std::string element = text.substr(first,last-first);
        if (!element.empty()) // Allow for extra nulls...
        {
            strings.push_back(element);
        }
        first = last+1;
    }
    return strings;
}
0
 
smitty1276Commented:
//this will move to the beginning of each string
//and copy them into emptyString (doesn't store them)
--------------------------------------------------------
char *testString = "string1\0string2\0string3\0\0";

char *ptr = testString;
char emptyString[20];

while( strlen(ptr) > 0 )
{
  strcpy( emptyString, ptr );
  ptr += strlen(ptr)+1;  //advance to beginning of next str
}
----------------------------------------------------
That while loop moves ptr to the beginning of each string.  You can do what you want with them.  Just for demonstration purposes, I copied each string into a char array called emptyString[].  When it gets to the "\0\0" it exits the loop... you've found all of the strings.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now