Solved

Extracting null-terminated strings

Posted on 2001-07-11
9
302 Views
Last Modified: 2012-06-22
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
Comment
Question by:paulburns
9 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6275867
0
 
LVL 2

Author Comment

by:paulburns
ID: 6275890
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
 
LVL 31

Accepted Solution

by:
Zoppo earned 50 total points
ID: 6276080
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 30

Expert Comment

by:Axter
ID: 6276129
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 6276136
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
 
LVL 30

Expert Comment

by:Axter
ID: 6276141
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
 
LVL 30

Expert Comment

by:Axter
ID: 6276150
>>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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6276193
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
 
LVL 2

Expert Comment

by:smitty1276
ID: 6276286
//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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question