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
3
votes
5 answers

CStringT to char[]

I'm trying to make changes to some legacy code. I need to fill a char[] ext with a file extension gotten using filename.Right(3). Problem is that I don't know how to convert from a CStringT to a char[]. There has to be a really easy solution that…
Justin
  • 9,419
  • 7
  • 34
  • 41
3
votes
2 answers

.Net c# marshalling of char *

I am trying to call an existing C dll from a C# application, using System.Runtime.InteropServices, and am having difficulty matching the signatures between the PInvoke function and the Target function. The target function is __declspec(dllexport)…
user1400716
  • 1,126
  • 5
  • 13
  • 32
3
votes
8 answers

Avoiding memory leaks while mutating c-strings

For educational purposes, I am using cstrings in some test programs. I would like to shorten strings with a placeholder such as "...". That is, "Quite a long string" will become "Quite a lo..." if my maximum length is set to 13. Further, I do not…
mats
  • 1,818
  • 3
  • 18
  • 26
3
votes
2 answers

How to get rid of string of characters in CString in C++

The CString type I am talking about is microsoft's. I've attempted two methods; Method 1) using the Remove() and Replace() function found on msdn website, and Visual c++ says Error:argument of type "const char*" is incompatible with argument of type…
minusatwelfth
  • 191
  • 6
  • 14
3
votes
4 answers

Need to delete CString after use to free memory?

If I am using a CString like this: void myFunc(char *str) { CString s(str); // Manipulate other data with CString // ... // Finished // Should I somehow delete 's' here to avoid a memory leak? } Is the string erased once the function…
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
3
votes
1 answer

Why does strcpy take it's arguments in reverse order

Almost every other program or function designed to copy one object into another uses the basic convention copy(source, destination) Even common logic would lead one to the conclusion that you are copying something FROM here to THERE. Why does strcpy…
charliehorse55
  • 1,940
  • 5
  • 24
  • 38
2
votes
3 answers

Why does MFC C++ CString(const char*) completely changing const char* value?

I hope the title was good enough to help explain what I am having problems with. I think once I solve this problem my project will be pretty much finished. Just a note, both projects are compiled under Unicode. I am working with a CLI/C++ DLL that…
Landin Martens
  • 3,283
  • 12
  • 43
  • 61
2
votes
3 answers

EXC_BAD_ACCESS error when using NSString getCString

I'm trying to parse some HTML. I use stringWithContentsOfURL to get the HTML. I attempt to load this into a character array so I can parse it, but I crash with the EXC_BAD_ACCESS error when getCString is called. Here is the relavent code: -…
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
2
votes
5 answers

How to use Templates when working with std::strings and c-style strings?

I was just messing around with templates, when I tried to do this: template void print_error(T msg) { #ifdef PLATFORM_WIN32 ::MessageBox(0, reinterpret_cast< LPCSTR >(msg), "Error", MB_ICONERROR|MB_OK); #else cout << msg <<…
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
2
votes
1 answer

using CString with boost string algorithms - reduce to one typedef?

I need to write some code for a MFC project, but I don't know how to get required code to work when using MFC. I prototyped my function first just using the STL types, and boost. STL Prototype #include #include…
Peter Nimmo
  • 1,045
  • 2
  • 12
  • 25
2
votes
5 answers

Write a unicode CString to a file using WriteFile API

How can I write contents of a CString instance to a file opened by CreateFile using WriteFile Win32 API function? Please note MFC is not used, and CString is used by including "atlstr.h" edit: Can just I do WriteFile(handle, cstr,…
Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
2
votes
3 answers

Conversion from CString to char*/TCHAR*

I am well aware of techniques to convert CString to a C-style character. One of them is to use strcpy/_tcscpy, and others include using CStrBuf. The problem: char Destination[100]; CStringA Source; // A is for simplicity and explicit ANSI…
Ajay
  • 18,086
  • 12
  • 59
  • 105
2
votes
0 answers

How to parse a string in c++

Possible Duplicate: How do I tokenize a string in C++? So I am trying to parse a string and remove each individual word and store it into a char*. Example: string theString = "The quick brown fox."; Turns into: char* c1 = "The" char* c2 =…
trev9065
  • 3,371
  • 4
  • 30
  • 45
2
votes
1 answer

Reading C character arrays into doubles

This is a very elementary question, but my C is very very rusty and I need a refresher. I have a string which is always in exactly the same format: di ###.# ###.# ###. I would like to read the first number into d1, the second into d2, and the third…
Adam S
  • 8,945
  • 17
  • 67
  • 103
2
votes
2 answers

Can CString::Format() receive const std::string?

Can CString::Format() receive const std::string? Example: void some_func( const std::string a_string ) { CString b_string("World"); CString c_string; c_string.Format("%s %s!", a_string, b_string); /* print c_string */ };
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359