Click here to Skip to main content
11,642,658 members (68,184 online)
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 16:28pm
Selva K2.6K
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 DamithSL 254
1 Sergey Alexandrovich Kryukov 204
2 OriginalGriff 160
3 Afzaal Ahmad Zeeshan 129
4 Mika Wendelius 85
0 Mika Wendelius 485
1 DamithSL 457
2 OriginalGriff 365
3 Afzaal Ahmad Zeeshan 258
4 Sergey Alexandrovich Kryukov 254

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 16 Nov 2011
Copyright © CodeProject, 1999-2015
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