Questions tagged [cstring]

Refers to 0-terminated strings as popularized by C, as well as the header-files `string.h` and `cstring`.

A C string is a sequence of non-zero bytes terminated by a NUL byte (0x00), usually used to store ASCII or UTF-8 text.

C strings are defined as char arrays in C and C++, where char is an 8-bit type. C strings can be referred to using a char pointer.

Extensions to the basic C string (e.g. to 16-bit wchar_t "wide strings") also exist in various programming environments.

In C++, the standard std::string class interoperates with C strings via the .c_str() method, though beware that std::strings can contain embedded 0-bytes.

488 questions
1
vote
2 answers

Subclassing MFC CString, losing implicit conversion when CString passed to format strings (sprintf, CString::FormatString, etc.)

I'm subclassing the MFC class CString (nothing wrong with the class, but trust me, I need to do this for specific implementation). I've succesfully customized some behaviors, but I noticed that I lost the implicit (LPCTSTR) operator that seems to…
1
vote
3 answers

Need help finding bug, if string input is composed all of same character one output character is corrupt

reverser() reverses a cstring (not in place). 99% of the time it works but some input corrupts it for example it appears if aStr2[] is assigned a string made up of the same character it will have an error. #include #include…
Celeritas
  • 14,489
  • 36
  • 113
  • 194
1
vote
2 answers

how to split a C++ string to get whole string individually and some parts/characters of it

My question is, how can I split a string in C++? For example, I have ` string str = "[ (a*b) + {(c-d)/f} ]" It need to get the whole expression individually like [,(,a,*,b,...... And I want to get the brackets only like [,(,),{,(,),},] on their…
Husnain Iqbal
  • 466
  • 6
  • 16
1
vote
2 answers

cannot convert parameter 1 from 'const char [6]' to 'const wchar_t *

I'm new to MFC and I don't know what to do with this error. ERROR error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [6]' to 'const wchar_t *' heres the line: m_Echo1.Format("%d…
user2668338
  • 13
  • 1
  • 4
1
vote
2 answers

C Stack-Allocated String Scope

For straight C and GCC, why doesn't the pointed-to string get corrupted here? #include int main(int argc, char *argv[]) { char* str_ptr = NULL; { //local to this scope-block char str[4]={0}; sprintf(str,…
1
vote
4 answers

Read CString from buffer with unknown length?

Let's say I have a file. I read all the bytes into an unsigned char buffer. From there I'm trying to read a c string (null terminated) without knowing it's length. I tried the following: char* Stream::ReadCString() { char str[0x10000]; int…
MysteryDev
  • 610
  • 2
  • 12
  • 31
1
vote
1 answer

Compile time string assignment for run-time identification

I've been using a pattern in a library I'm creating that uses passes a String name of an object to its base object's constructor. I've tried using std::string and c-style strings but keep getting weird memory errors with Valgrind. class Base…
h4tch
  • 264
  • 1
  • 8
1
vote
3 answers

Create string class without strings library

I am trying to make my own string class without using the strings library ie. #include I am allowed to use c-style strings! I have to make the iString ctor, copy ctor, dtor, and overload the operators >>, <<, +, * My problem so far is that…
1
vote
1 answer

Creating a dynamic Cstring

I am facing this position where I need to create a dynamic string of user-inputted size (so I tried used a dynamic cstring). char * S; int x; cin >> x; S = new char[x]; for (int i = 0; i < x; i++) { S[i]=' '; //trying to make it a…
Ezz
  • 534
  • 2
  • 8
  • 17
1
vote
1 answer

Multibyte CString to Unicode MFC

I have 2 MFC projects. A library project uses Multi-byte Character set. An executable project uses Unicode Character set. I only wrote the library project and have to use it in the executable. One of the library's functions returns a CString.…
irco
  • 961
  • 6
  • 13
  • 27
1
vote
2 answers

how to convert CString to Bytes

i am actually tryin to convert a csharp code to c... below is the C# code.. CString data = "world is beautiful"; Byte[] quote = ASCIIEncoding.UTF8.GetBytes(data); in the above code... it converts the string into bytes..similarily is ther a way…
kiddo
  • 1,596
  • 7
  • 31
  • 60
1
vote
1 answer

Convert Wstring to CString

i have a variable of Cstring,need to convert it to wstring.
Nitin
  • 11
  • 3
1
vote
2 answers

I am getting a bunch of extra chars when i try to reverse a cstring in c++

The string entered by the user appears to be reversed but it is also followed by a bunch of garbage chars. Here is my reverse function: void reverse(char str[]) { char reversed[MAX_CHAR]; for(int i = 0; i < strlen(str); i++) { …
chillpenguin
  • 2,989
  • 2
  • 15
  • 18
1
vote
3 answers

CString or char array which one is better in terms of memory

I read somewhere that usage of CString is costly. Can you calrify it with an example. Also among CString and char array, which is better in terms of memory.
CodeRider
  • 1,750
  • 6
  • 18
  • 34
1
vote
0 answers

How to find a character string within a string?

It's only every once in awhile that I have to write in c++. My problem is how do you find a character string within a string? Then save that line, and then look for a set of numbers in that line. For example I have a text file that looks like…