Solved

Extracting null-terminated strings

Posted on 2001-07-11
9
299 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 30

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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now