std::erase issue with WCHAR

Is there a way to ignore the first character, when using the command below?

I just want to get rid of the "\" that is at the beginning of szLnk.


int wmain(int argc, const WCHAR *argv[])
{
....

WCHAR szLnk[1024];

_snwprintf_s(szLnk, sizeof(szLnk), wcschr((const wchar_t*)argv[3], L'\\'));

szLnk.erase(0,1); <- throws "expression must have class type" error

....

}


Any ideas?
Last edited on
szLnk is an array of WChar. Arrays do not have member functions, so it does not have an erase member function.

This might work for you:

1
2
3
4
5
6
7
8
9
10
11
12
int wmain(int argc, const WCHAR *argv[])
{
  ....

  WCHAR szLnk[1024];

  _snwprintf_s(szLnk, sizeof(szLnk), wcschr((const wchar_t*)argv[3], L'\\'));

  szLnk++; // now, szLnk is pointing at the SECOND wchar in the array. 
  ....

}


or you could change your _snwprintf_s function call so that you don't start from the beginning of the array you're copying, but skip the first character to begin with.

Or create another array, WCHAR szLnk2[1024];, and copy into it everything except the first character:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int wmain(int argc, const WCHAR *argv[])
{
  ....

  WCHAR szLnk[1024];

  _snwprintf_s(szLnk, sizeof(szLnk), wcschr((const wchar_t*)argv[3], L'\\'));

  WCHAR szLnk2[1024];

  for (int i = 1; i < 1024; ++i)
  {
    szLnk2[i-1]= szLnk[i];
  }


}


Lots of options. The big mistake you're making is thinking that szLnk is some kind of C++ object with member functions like erase. It isn't. It's a plain array.
I'm not very familiar with the "_snwprintf_s" function call, do you have a sample of how to do what you suggested?

".. change your _snwprintf_s function call so that you don't start from the beginning .."

I tried this, but it didn't work;



_snwprintf_s(szLnk, sizeof(szLnk)-1, wcschr((const wchar_t*)argv[3], L'\\'));



Last edited on
@Repeater, You can't increment an array name. And _snwprintf_s needs the size in words (not chars/bytes). So maybe something like:
1
2
3
4
    WCHAR lnk[1024];
    auto p = wcschr(argv[3], L'\\');
    if (!p) { /* error: no backslash in third argument */ }
    _snwprintf_s(lnk, sizeof lnk / sizeof lnk[0], p + 1);

Yeah, blah blah, make a pointer to it and increment that.
Repeater wrote:
Yeah, blah blah, make a pointer to it and increment that.

Duh, details don't matter in programming, duh!
Your answer was crap.
Period.
Suck it up, tpb.
@tpb, works like a charm man!

Many thanks!

Repeater wrote:
Suck it up, tpb.

I suggest you hide in the corner and sob for a little while, baby.
Now tell me you're a navy seal and you're going to track me down, but do it by private mail so nobody else has to put up with it. I won't read it, but you'll feel better.
Whine, whine, whine. Whimper elsewhere, child.
Topic archived. No new replies allowed.