This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I am trying to use the std::rename() function to move a .docx file, however, the name of the file may vary. How can I use a std::string within std::rename() so that it does not have to be a hardcoded filepath, like this std::rename(filepath, destination);?
I don't know how you want to populate the strings in question, but here you go:
std::string fromName {"whatever you're going to do"};
std::string toName {"whatever you're going to do"};
std::rename(fromName.c_str(), toName.c_str());
Related
This question already has answers here:
printf with std::string?
(9 answers)
Closed 7 months ago.
std::string sszModName = "kernel32.dll";
std::string WinVersion = "WIN81";
std::string MachineGUID= "ce9e95db-5fda-436a-b29a-f5537702c77d";
char buf[1024];
sprintf(buf, "https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s", sszModName, "ERROR_HASH_VERIFY", WinVersion, MachineGUID);
This code causes program lag, could you help me figure out why?
Try
sprintf(buf,
"https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s",
sszModName.c_str(),
"ERROR_HASH_VERIFY",
WinVersion.c_str(),
MachineGUID.c_str());
C strings are not the same as C++ strings. spprintf only uses C strings so you must use .c_str() to turn your C++ strings into C strings.
This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";
This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I want to create sth like:
int main ()
{
string s{};
std::cout << "Enter CMD: \n";
getline(cin,s);
system(s);
}
But since I can use only const char on system, its not working at all, is there any different solution to this? mabye shellexecute?
You can use std::string::c_str().
system(s.c_str());
This question already has answers here:
Convert string to variable name or variable type
(7 answers)
Closed 7 years ago.
So basically what I'm trying to find out, take this scenario:
std::string input;
Ask user for a string?: Apples
cin >> input;
std::string Apples = "input";
So basically, I ask the user for a string, and then create a variable with name of that string. Is this possible?
No, you can't do that.
The closest functionality is to use a map.
std::map<std::string, int> aMap;
This question already has answers here:
How to convert std::string to LPCSTR?
(9 answers)
Closed 8 years ago.
I have a list of file names in a .txt document, and I would like to move each of these files from one folder to another.
Using MoveFileA() I am getting the error, "no suitable conversion between std::string and LCPSTR".
Here is my code, after opening up my .txt file:
while (std::getline(myfile, line))
{
std::string oldLocation = "C:\\Users\\name\\Desktop\\docs\\folder1\\" + line;
std::string newLocation = "C:\\Users\\name\\Desktop\\docs\\folder2\\" + line;
MoveFileA(oldLocation, newLocation);
}
If I type in the full path as arguments for MoveFileA, instead of sending it a variable, it works but I am unable to iterate over .txt file this way.
Any suggestions on how I might fix this?
LCPSTR means long constant pointer to a string, which means it's a null terminated c string.
std::string is an object. It is something different. But it luckily provides a convenience method c_str the provides a pointer to a constant c style string. So as the comment says you should go by:
MoveFileA(oldLocation.c_str(), newLocation.c_str());
It is worth of explicitly noting, that you can't drop it in every place instead of char*, but only when the string won't be modified. It returns const char*. This is where the C in LCPSTR gets important.