Solved

Extracting null-terminated strings

Posted on 2001-07-11
9
300 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

911 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

23 Experts available now in Live!

Get 1:1 Help Now