Gulam:
I believe you are taking an unnecessarily circuitous route. This is what I do:
(a) First, use the C++ type "string" and with it, do all manipulations, substring concatenations, extractions, etc. Those operations are trivially easy done with string, plus you get extra security (overrunning long paths, etc)
(b) Next, use relatively simple code like the one attached.
That code is part of all my console/shell applications; it works as intended under different operating systems.
=======================
Auxiliary portable functions: