Click here to Skip to main content
15,942,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to tekenize the string returned in the below code

wchar_t m_VStrBuf[2048];

m_vNumChar=GetPrivateProfileString(_T("List1"),NULL,NULL,m_VStrBuf,2048,m_VFileName);


The buffer(m_VFileName) is filled with one or more null-terminated strings; the last string is followed by a second null character.
Posted
Comments
Chandrasekharan P 15-Nov-11 23:14pm    
Do you know what is the separator??

wchar_t *ts;

ts = m_VFileName;
while (wcslen(ts) != 0)
{
  //process string ts
  ts += wcslen(ts) + sizeof(wchar_t);  // skip to next string in result buffer
}


Typed but not run / debugged (but it should be close)
[edit - skip over the null too]
 
Share this answer
 
v2
Comments
Orjan Westin 16-Nov-11 5:30am    
Almost - you need to increment with 1 to get past the \0 terminator, otherwise the second call will always return 0, since the first character is \0
Chuck O'Toole 16-Nov-11 11:27am    
whoops, had that in my real code, forgot to type it here. Thanks, I updated the solution to reflect this.
You can use strtok(). like this
char * pch;

pch = strtok (m_VStrBuf,","); // Here , is seprator.
while (pch != NULL)
{
  pch = strtok (NULL, ",");
}

Note : To fetch value from file u have to stored the value with seprator.
 
Share this answer
 
Comments
Selva K 16-Nov-11 0:16am    
Its not a char its wchar and the seperator is NULL
Assuming you want to keep the strings, you can use this (tested) function.
C++
void tokenise_wstr(const wchar_t * str, size_t len, 
               std::vector<std::wstring>& strings)
{
  size_t prev = 0;
  size_t next = wcslen(str);

  while ( (prev < next ) && (next < len))
  {
    strings.push_back(
      std::wstring(str + prev, str + next + 1));
    prev = next + 1;
    next = prev + wcslen(str + prev);
  }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900