How to copy part of CString to a new one? [duplicate] - c++

This question already has answers here:
mfc copy certain sections of a CString
(2 answers)
Closed 7 years ago.
I got a CString str
I want to copy str[3]~ str[5] to a brand new one.
I tried C++ char* 's method, not compatible.
So what is the right way do this in VC++
Thank you,

Have you tried using the the MID function?
str.Mid( 3, 3 ) should give you the substring you are looking for.
Updated reference: http://msdn.microsoft.com/en-us/library/b4c90k3d(v=vs.80).aspx

Related

How do i "subtract" a character from a string in c++ [duplicate]

This question already has answers here:
Remove last character from C++ string
(12 answers)
Closed 8 months ago.
Lets say I have a string variable with the value "bananas" in it. I want to subtract the last letter so the string becomes "banana". I am quite a newbie, so I dont even know how to tackle this.
Just use the pop_back() function.
Try this code, it 'subtracts' the last character:
std::string str = "bananas";
str.pop_back();

Cpp create string creation without initialization [duplicate]

This question already has answers here:
How do I read an entire file into a std::string in C++?
(23 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I want to read the contents of a file into a string.
string contents(size, '\0'); -> size was determined above using the file.seekg and file.tellg.
file.read((char*) contents.data(), size);
Now, I know that the contents of the string will be overwritten in file.read, so there's no need to initialize the the string to null characters.
Is there a way to do so?
You can do this:
std::string contents(std::istreambuf_iterator<char>{file},
std::istreambuf_iterator<char>{});
But it may not be faster. Either way the initialization is likely to be very fast in comparison to reading from the drive.

Explain what for (char c : str) does? [duplicate]

This question already has answers here:
'colon' and 'auto' in for loop c++? need some help understanding the syntax
(3 answers)
colon in for loop in C++
(1 answer)
Closed 3 years ago.
I am asking since I couldnt immediately find an answer on google, I know the answer is simple.
what does for (char c : str) {} do in a for loop?
Thank you!
It iterates the individual characters of str, copying each one to the (local) variable c for use in each iteration of the loop.

Using The va_list to generate a string [duplicate]

This question already has answers here:
Variable number of arguments in C++?
(17 answers)
Variable number of parameters in function in C++
(8 answers)
Closed 3 years ago.
Im using c++ to create a custom monitor using the lvgl library. So ive saw that the "..." can handle infinite variables. I wanted to create a function to convert the string and the following parameters into a pure string. I want the function to take in a character pointer, then the "...". I want it to take out the "%d" parts of the character pointer and replace it with the corresponding value in the va_list. If the va_list is empty, it can return the same character pointer. How can I achieve this? i have no knowledge about the "...", i only know they are called varidic functions.
Thanks all for your kind help!

Getting a path and appending a filename [duplicate]

This question already has answers here:
How do I concatenate two strings in C?
(12 answers)
How to concatenate two strings in C++?
(8 answers)
Closed 6 years ago.
I'm trying to get a path (appdata) and append a filename (smss.dll), and combine these two to form C:\users\username\appdata\roaming\smss.dll
I have this already:
static char appdata[MAX_PATH+1];
SHGetSpecialFolderPathA(HWND_DESKTOP, appdata, CSIDL_APPDATA, FALSE);
LPCSTR target = "smss.dll";
How can I combine these to into one variable?
You can use PathAppend (limited to MAX_PATH characters), or PathCchAppendEx starting with Windows 8.
static char appdata[MAX_PATH+1];
SHGetSpecialFolderPathA(HWND_DESKTOP, appdata, CSIDL_APPDATA, FALSE);
strcat(appdata, "\\smss.dll");
The strcat function appends the right-hand string to the left-hand buffer. It assumes the destination has room for the concatenation to be done. If there is not enough space, it is undefined behavior. Thus it is a good idea to check that there is enough space.