0

I have a string say

 LPTSTR orgString =_T("abc\refg\rhij");

Here task is I need to remove a character '\r'(carriage return). I don't want to use again new temporary buffer, using existing string with some manipulations I need to remove '\r' character.

But while moving the contents inside the "orgString" I am getting access violation exception.

Even simple statement orgString[3] = orgString[4]; giving access violation.

Don't know where I am thinking wrong please help me!!!

cpp_learner
  • 362
  • 2
  • 4
  • 14
  • `orgString` points to (the first character of) a string constant. Modifying a string constant is undefined behavior. The declaration will not compile with a fully conforming C++11 or later compiler. For pure Windows programming use `wchar_t` based text handling. Then the right tool for you here is the `wstring` type, from the `` header. Don't use the silly Windows 9x compatibility macros like `_T`. Compile with the `UNICODE` symbol defined. – Cheers and hth. - Alf Apr 26 '16 at 08:55
  • No I don't want to use string.h – cpp_learner Apr 26 '16 at 08:58
  • Possible duplicate of [this](http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c) – Mohamad Elghawi Apr 26 '16 at 08:59
  • but what I read is... LPTSTR will allow string modification unlike LPCSTR – cpp_learner Apr 26 '16 at 09:02
  • The declaration should better look like `wstring orgString = L"abc\refg\rhij";` – Cheers and hth. - Alf Apr 26 '16 at 09:09
  • I need to return LPTSTR type only so that other function will take it as input. – cpp_learner Apr 26 '16 at 09:17

0 Answers0