0

I have a TCHAR string which contains a path. I need to replace all occurences, if any, of / with \ in the path. The variable holding the path is defined as follows:

TCHAR mypath[1024];

If mypath contains C:/new/newfile/a.txt, then I would need the / to be replaced by \ so that the resulting string becomes C:\new\newfile\a.txt.

Also, the file that I need to adapt already has UNICODE defined as follows:

#ifndef UNICODE
#define UNICODE
#endif

And functions like wcsncmp have been used for string comparison. How can I achieve character replacement in a TCHAR string?

honk
  • 9,137
  • 11
  • 75
  • 83
SoDa
  • 71
  • 2
  • 8
  • 1
    Why use a `TCHAR[]` in the first place? Stick with a `std::wstring` and use `std::replace`. – chris Nov 12 '13 at 06:17
  • Yes, these days TCHAR is obsolete. No reason for it in a modern C++ program. – john Nov 12 '13 at 07:09
  • 1
    @john I don't see anywhere specified that this is a modern C++ program. Maybe (s)he is maintaining an old program. – ajcaruana Nov 12 '13 at 07:53

2 Answers2

3

Try the following code:

TCHAR *pCH = mypath;
while ( ( pCH = _tcschr( pCH, _T('/') ) ) != NULL )
{
    *pCH = _T('\\');
}
ajcaruana
  • 505
  • 3
  • 14
  • Please be aware that `_T()` yields a string. If you want to wrap characters correctly then you should use `_TCHAR()` or `TCHAR()` instead. – honk Aug 02 '14 at 16:07
  • @honk well, on my system `_T()` is defined as `#define _T(x) __T(x)`, which is defined as `#define __T(x) x` or `#define __T(x) L ## x` depending on whether `UNICODE` is defined in your project. `L"ABC"` is a string, but `L'x'` is a (wide) character, but the macro `_T()` can be used for both cases. – ajcaruana Aug 04 '14 at 07:50
  • You are right, your code is perfectly valid. I guess my comment was guided by my personal coding preferences where I try to be as explicit as possible. Sorry for bothering! – honk Aug 04 '14 at 19:26
1

The path in Windows OS is at most 260 characters. So, I think one of the way is to do brute force finding/replacing.

#include <stdlib.h>
TCHAR myPath[_MAX_PATH];

for (int i=0; i<_MAX_PATH && myPath[i]; i++)
{
    if (myPath[i] == '/')
       myPath[i] = '\\';
}
Deidrei
  • 2,125
  • 1
  • 14
  • 14
  • Yeah, MAX_PATH can be replaced by _MAX_PATH, by #include . Thanks. – Deidrei Nov 12 '13 at 07:05
  • I was referring to the part where it points out that the path can really be up to 32768 characters. – Jerry Coffin Nov 12 '13 at 07:09
  • I saw it, "Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Server 2003, and Windows Vista NTFS file system supports paths up to 32768 characters in length, but only when using the Unicode APIs". But when I look at the definition of _MAX_PATH in VS 2010, I only see #define _MAX_PATH 260 – Deidrei Nov 12 '13 at 07:15
  • Here is a part in stdlib.h, #define _MAX_PATH 260 /* max. length of full pathname */ #define _MAX_DRIVE 3 /* max. length of drive component */ #define _MAX_DIR 256 /* max. length of path component */ #define _MAX_FNAME 256 /* max. length of file name component */ #define _MAX_EXT 256 /* max. length of extension component */ – Deidrei Nov 12 '13 at 07:17
  • 1
    You really should check for the end of the string (terminating null-character): `myPath[i] && i < _MAX_PATH` – klaus triendl Nov 12 '13 at 08:07
  • If you want to handle the TCHAR thingy correctly then you should wrap the characters `'/'` and `'\\'` by using `_TCHAR()` or `TCHAR()`. – honk Aug 02 '14 at 16:12