如何在 C++ 中将 std::string 转换为 const char
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20390008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert std::string to const char in C++
提问by PritishC
I tried to research this for a bit and I can't figure out the problem
我试着研究了一下,但我无法弄清楚问题所在
Here's the code:
这是代码:
#include <iostream>
#include <stdlib.h>
#include <string>
void choose();
void newuser();
void admuse();
using namespace std;
string x;
string z;
string w;
void CreNeAcc(){
cout << "Enter User Name for your new account\n";
getline(cin, x);
cout << "Enter Password for your new account\n";
getline(cin, z);
cout << "Would you like the account to be admin?\n";
cout << "Yes = Y, No = N\n";
getline(cin, w);
choose();
}
void choose(){
if(w == "Y"){
newuser();
admuse();
}else if(w == "N"){
newuser();
}else{
cout << "Invalide Command\n";
}
}
void newuser(){
const char* Letter_x = x.c_str();
char command [100] = "net user /add ";
strcat(command, x); //This is where I get the error
strcat(command, " ");
strcat(commad, z);
system(command);
}
void admuse(){
system("new localgroup administrators " << x << " /add")
}
also the error its giving me is:
它给我的错误也是:
cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '2' to 'char* strcat(char*, const char*)'|
回答by Matt Reynolds
You have to use c_str()
(see here). It's implemented by simply apending it to your std::string
, like so:
您必须使用c_str()
(请参阅此处)。它是通过简单地将它附加到您的 来实现的std::string
,如下所示:
string myFavFruit = "Pineapple"
const char* foo = myFavFruit.c_str();
strcat(command, foo);
Actually, you have everything there you're just not using your const char*
variable in the strcat()
's arguments. You define Letter_x
, but then use x
in the function instead. Rewrite your newuser()
as follows:
实际上,您拥有一切,只是没有const char*
在strcat()
的参数中使用变量。您定义Letter_x
,然后x
在函数中使用。重写你的newuser()
如下:
void newuser(){
const char* Letter_x = x.c_str();
char command [100] = "net user /add ";
strcat(command, Letter_x); //Here, use 'Letter_x' instead of 'x'
strcat(command, " ");
strcat(command, z); //You will also need to do your 'c_str()' conversion on z before you can use it here, otherwise you'll have the same error as before.
system(command);
}
Lastly, you can avoid all this because you can append strings simply using the +=
operator (see here). Try this:
最后,您可以避免所有这些,因为您只需使用+=
运算符即可附加字符串(请参阅此处)。尝试这个:
string command = "net user /add ";
command += x;
command += " ";
command += z;
回答by Pawe? Stawarz
In order to convert a string
into an const char*
use c_str()
.
为了将 astring
转换为const char*
use c_str()
。
On a side note tho:why are you using strcat
in the first place? You can concatenate strings with the operator+
.
附带说明一下:您strcat
首先为什么要使用?您可以将字符串与operator+
.
string a = "try", b = " this";
string c = a+b; // "try this"
回答by PritishC
You might be looking for this: How to convert a char* pointer into a C++ string?
您可能正在寻找:如何将 char* 指针转换为 C++ 字符串?
According to the link, you may use c_str()
to return a pointer to a null terminated char array version of your string.
根据链接,您可以使用c_str()
返回指向字符串的空终止字符数组版本的指针。