问题描述
我的程序中有这段代码。我正在尝试发送一个存储在buf变量中的RSA密钥,但是c_str()似乎剪切了存储在buf中的字符串...
int client :: sendData( SOCKET& ss,std :: string buf){
if(cipher)buf = client_rsa-> encrypt(buf);
char sendbuf [DEFAULT_BUFLEN];
strcpy_s(sendbuf,buf.c_str());
std :: cout<< BUF: << buf<<" \ nSENDBUF:" << sendbuf<< " \ n";
int iSendResult = send(ss,sendbuf,buf.length(),0);
if(iSendResult == SOCKET_ERROR){
printf(" send failed:%d \ n",WSAGetLastError());
closesocket(ss);
WSACleanup() ;
返回1;
}
返回0;
}
以下是我在运行此功能后在控制台上获得的信息:
BUF:♠☻Ą
$ b $b꼣ŚĚ?►ĹUx↑a↕ęy? $úP?Ł×V?Ű..Ů∟é═pů,ü¨Ak⌂?ăů↕ĺxw▓►ŔD;▼<♂▀&?Č6Č,ŘG+§ťĘă
SENDBUF:♠☻
只复制第一个字符...任何人都知道如何解决这个问题......或者不使用c_str()函数复制它。我将非常感谢任何帮助。
I have this piece of code in my program. I''m trying to send an RSA key which is stored in buf variable here, but c_str() seems to cut string stored in buf...
int client::sendData(SOCKET &ss, std::string buf) {
if (cipher) buf = client_rsa->encrypt(buf);
char sendbuf[DEFAULT_BUFLEN];
strcpy_s(sendbuf, buf.c_str());
std::cout << "BUF: " << buf <<"\nSENDBUF: " << sendbuf << "\n";
int iSendResult = send( ss, sendbuf, buf.length(), 0 );
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ss);
WSACleanup();
return 1;
}
return 0;
}
Here is what I get on the console after running this function:
BUF: ♠☻ Ą
꼣ŚĚ?►ĹUx↑a↕ęy?$úP?Ł×V?Ű..Ů∟é═pů,ü¨Ak⌂?ăů↕ĺxw▓►ŔD ;▼<♂▀&?╔6Č,ŘG+§ťĘă
SENDBUF: ♠☻
Only first to chars are copied... Anyone has any idea how to fix this... or copy it without using c_str() function. I''ll be very grateful for any help.
推荐答案
这篇关于c_str()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!