开发者

Parsing list of strings ending with double NULL [closed]

开发者 https://www.devze.com 2023-03-23 22:45 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in 开发者_运维问答its current form.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in 开发者_运维问答its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How to parse in c++ list of null-terminated Unicode strings where the list is terminated with two NULL characters?


There's this little example on Raymond Chen's blog (which, perhaps not surprisingly, is the first find in Google for "double null terminated string"):

This reinterpretation of a double-null-terminated string as really a list of strings with an empty string as the terminator makes writing code to walk through a double-null-terminated string quite straightforward:

> for (LPTSTR pszz = pszzStart; *pszz; pszz += lstrlen(pszz) + 1) {  ...
> do something with pszz ... }

The LPTSTR and lstrlen are wrappers which change depending on whether or not _UNICODE is set.


You simply build a list of strings and abort when one is empty:

std::vector<std::string> result;
result.push_back( std::string() );
while (std::cin) {
    char c = std::cin.get();
    if ( c == 0 ) { 
        if ( result.back().empty() ) { result.pop_back(); return; }
        else result.push_back(std::string()); }
    } else {
        result.back().push_back(c);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消