Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: VS2005 C++ MFC
How to tekenize the string returned in the below code
wchar_t m_VStrBuf[2048];
The buffer(m_VFileName) is filled with one or more null-terminated strings; the last string is followed by a second null character.
Posted 15-Nov-11 17:28pm
Selva K2.4K
Chandrasekharan P at 15-Nov-11 23:14pm
Do you know what is the separator??
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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]
Cool Cow Orjan at 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 at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
selvapk at 16-Nov-11 0:16am
Its not a char its wchar and the seperator is NULL
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Assuming you want to keep the strings, you can use this (tested) function.
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))
      std::wstring(str + prev, str + next + 1));
    prev = next + 1;
    next = prev + wcslen(str + prev);

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310

Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 16 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100