How to split char pointer with multiple delimiters & return array of char pointers in c++? -


in duplicate of question split char* char * array advised use string rather char*. need work lpwstr. since it's typedef of char*, prefer use char*. tried following code, gives wrong output:

char**splitbymultipledelimiters(char*ori,char deli[],int lengthofdelimiterarray) {     char*copy = ori;     char** strarray = new char*[10];     int j = 0;     int offset = 0;     char*word = (char*)malloc(50);     int length;     int split = 0;     for(int = 0; < (int)strlen(ori); i++)     {         for(int k = 0; (k < lengthofdelimiterarray) && (split == 0);k++)         {             if(ori[i] == deli[k])             {                 split = 1;             }         }         if(split == 1)//ori[i] == deli[0]         {             length = - offset;             strncpy(word,copy,length);             word[length] = '\0';             strarray[j] = word;             copy = ori + + 1;             //cout << "copy: " << copy << endl;             //cout << strarray[j] << endl;             j++;             offset = + 1;             split = 0;         }     }     strarray[j] = copy;    // string strarraytoreturn[j+1];     for(int = 0; < j+1; i++)     {         //strarraytoreturn[i] = strarray[i];         cout << strarray[i] << endl;     }     return strarray; }  void main() {         char*ori = "this:is\nmy:tst?why hate";         char deli[] = {':','?',' ','\n'};          int lengthofdelimiterarray = (sizeof(deli)/sizeof(*deli));         splitbymultipledelimiters(ori,deli,lengthofdelimiterarray); } 

are there other ways split lpwstr?

wait, talking about? don't see lpwstr anywhere in code. trying convert lpwstr? if so, there's standard library function that. there's standard library-based solution splitting on multiple chars. together, code might this:

#include <codecvt> #include <cstdio> #include <locale> #include <sstream> #include <string>  using std::string; using std::wstring;  wstring towide(const string &original) {     std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;     return converter.from_bytes(narrow_utf8_source_string); }  std::vector<wstring> splitmany(const string &original, const string &delimiters) {     std::stringstream stream(original);     std::string line;      while (std::getline(original, line))      {         std::size_t prev = 0, pos;         while ((pos = line.find_first_of(delimeters, prev)) != std::string::npos)         {             if (pos > prev)                 wordvector.push_back(line.substr(prev, pos-prev));             prev = pos + 1;         }         if (prev < line.length())             wordvector.push_back(line.substr(prev, std::string::npos));     } }  int main() {     string original = "this:is\nmy:tst?why hate";     string separators = ":? \n"      std::vector<wstring> results = splitmany(original, separators); } 

this code uses standard library these functions , less error-prone doing manually.

good luck!


edit: clear, wstring == lpwstr == wchar_t*.

edit 2: convert string wstring:

#include <codecvt> #include <locale> #include <string>  using std::string; using std::wstring;  string tomultibyte(const wstring &original) {     std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;     return converter.to_bytes(original); } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -