A C string is a string of characters completed by a null terminator. So since you have:
const char* data = "[caminho da pasta]\0[arquivo 1]\0[arquivo 2]\0[arquivo 3]\0\0";
You can calculate the size of the first string using the strlen
:
const char* str1 = data;
int len1 = strlen(str1);
The second string will start after the first and after the null terminator, like this:
const char* str2 = str1 + len1 + 1;
int len2 = strlen(str2);
And so on. You stop when the len
return zero, after all the end is marked by a double null marker.
You can do it in a loop like this:
int main() {
const char* data = "[caminho da pasta]\0[arquivo 1]\0[arquivo 2]\0[arquivo 3]\0\0";
vector<string> files;
const char* str = data;
int len = strlen(str);
while (len) {
files.push_back(string(str, len));
str = str + len + 1;
len = strlen(str);
}
for (unsigned i = 0; i < files.size(); ++i) {
cout << "[" << i << "] = '" << files[i] << "'" << endl;
}
return 0;
}
Resulting in:
[0] = '[caminho da pasta]'
[1] = '[arquivo 1]'
[2] = '[arquivo 2]'
[3] = '[arquivo 3]'
Functional example (coliru)
and this type of loop would work for a wchar_t string?
– Raphael Rosa
@Raphaelrosa Yes, just use
wcslen
in place ofstrlen
.– Guilherme Bernal
cool... thanks even dude
– Raphael Rosa