Solved

Extracting null-terminated strings

Posted on 2001-07-11
9
301 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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