0

I'm using a statement like

TCHAR path="";
wcscpy(path,_T("C:\\Users\\user\\Desktop\\Filename.ext"));

But its getting copied with spaces inbetween every character.How can i prevent it?

Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
abejoe
  • 153
  • 2
  • 9
  • Could you provide some compilable example reproducing the problem? – Lol4t0 Oct 23 '15 at 09:16
  • 1
    Did you forget to escape your backslashes or is that just a typo? – Mohamad Elghawi Oct 23 '15 at 09:17
  • Its a part of a huge project. Just need clarification on this part. Ask me questions on this for further clarity. I must strictly use only unicode character set. Just want to know is any other functions that can copy without spaces in between. – abejoe Oct 23 '15 at 09:20
  • @ Mohamad Elghawi : Its just a typo – abejoe Oct 23 '15 at 09:21
  • Where/how are you seeing spaces. Could it be that you are seeing a NULL character after each character? – Mohamad Elghawi Oct 23 '15 at 09:25
  • TCHAR can be either char or wchar_t depending on your compiler settings. Since wscpy requires wchar_t pointers, your use of TCHAR is both redundant and dangerous. It is either wchar_t, in which case you could have just used wchar_t, or char, in which case the program is invalid. – n. m. could be an AI Oct 23 '15 at 09:26
  • @Mohamad Elghawi : Yeah i'm seeing NULL characters in between. – abejoe Oct 23 '15 at 09:28
  • Sorry its not declared as TCHAR path=" ".But as TCHAR path[255]; – abejoe Oct 23 '15 at 09:33
  • @n.m. I tried with wchar_t. But still its the same problem. There are spaces inbetween. – abejoe Oct 23 '15 at 09:35
  • 1
    How do you display the result? Also, don't use `wcscpy` with `TCHAR` or `_T`, use `_tcscpy`. – Sebastian Redl Oct 23 '15 at 09:38
  • Of course it has the same effect. It must either be the same, or fail altogether. That is, TCHAR introduces uncertainty to your code and nothing else. Which is what was trying to tell you. There are no spaces in between. You are misinterpreting your observations. Show an MCVE. – n. m. could be an AI Oct 23 '15 at 09:39
  • @ Sebastian Redl : I tried with _tcscpy too but still its the same result. – abejoe Oct 23 '15 at 09:42
  • @ n.m : I use visual studio 13. and while debugging i can see the memory as data is getting copied with null characters inbetween – abejoe Oct 23 '15 at 09:43
  • @abejoe: Yes, there are NULL bytes in between, and that is perfectly normal in 16bit strings. It will only cause problems if you are processing 16bit strings as 8bit data instead of 16bit data. – Remy Lebeau Oct 26 '15 at 20:40

1 Answers1

0

You are NOT seeing spaces in between the characters.

wchar_t is 2 bytes on Windows (4 bytes on other platforms). If you wish to use it to hold any ASCII character (ie: characters 0-127, which only require 1 byte), you will see that the first byte holds the character value and the additional bytes hold NULL bytes, since we only need 1 byte to hold the value (or 7 bits, to be more precise). If you are looking at the string data as 8bit instead of 16bit/32bit, those NULLs will appear as spaces.

If you do not want this, use the char type instead.

On Windows, it is recommended to use wchar_t and std::wstring instead of char and std::string, especially when using the Win32 API since it will convert multi-byte strings to wide strings anyway.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mohamad Elghawi
  • 2,071
  • 10
  • 14