Getting a path and appending a filename [duplicate] - c++

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.

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();

C++ string length [duplicate]

This question already has answers here:
What does the C++ standard state the size of int, long type to be?
(24 answers)
Getting the actual length of a UTF-8 encoded std::string?
(11 answers)
Closed 2 years ago.
I was reading the documentation of string::length and, as you can see, it says that
Returns the length of the string, in terms of bytes.
So my question is, is this ensured to be also the number of char that it contains?
I know that usually a char is 1 byte, but is this ensured somewhere? Like in the standard or somewhere else?

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!

C++ strings vs vector<char> [duplicate]

This question already has answers here:
What are differences between std::string and std::vector<char>?
(5 answers)
C++: char test[100] vs array<char, 100> vs string
(4 answers)
Closed 7 years ago.
It's known to everyone of us that we should prefer string class in C++ for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string is for characters, vector is for other data types & classes because it shows great performance.
However is there any situation where we would need to prefer vector<char> (which I see seldom) over string ?
I'd use vector<char> only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>.
To be clear: it is all about expressing the intent.
To put it briefly: if you're storing text, then string, otherwise vector<char>.

Meaning of L near text in define [duplicate]

This question already has answers here:
What exactly is the L prefix in C++?
(3 answers)
Why is letter L used to indicate wide chars strings?
(2 answers)
Closed 8 years ago.
I came across something I can't search for what it means.
#define DEFAULT_KEY L"text"
What does 'L' mean or do?
Other example that I found
wcscat(xpath, L"\\");
It means the string literal has type const wchar_t*, which is a type different that const char*. It is usually used to store Unicode strings.
Some APIs (particularly the Windows API) use this type all over the place.
The L prefix denotes a wide character/string literal; i.e., it is of type wchar_t instead of char. Unicode based programs typically use wide strings, while ANSI/ASCII based programs typically do not.