我非常喜歡%s,ID的概念。我注意到這與char有效,我知道這是一個C風格的特性。我在想,如果那裏有任何其他方式來實現%s的字符串:%s不會使用字符串
繼承人我的代碼:
#include "services.h"
#include <iostream>
using namespace std;
services::services()
{
}
services::~services()
{
}
int services::startup()
{
#if defined(WINDOWS)
{
WSAData data;
if(WSAStartup(MAKEWORD(2,2), &data) != 0)
exit(1);
}
#endif
int addresssize = sizeof(address);
address.sin_addr.s_addr = inet_addr("69.60.118.163");
address.sin_family = AF_INET;
address.sin_port = htons(6667);
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
return -1;
string PASSWORD = "password1";
string SERVER_NAME = "admin.services.net";
string SERVER_DESC = "Administration IRC Services";
if(connect(sock,(struct sockaddr *)&address,addresssize) == 0)
{
send_cmd("PASS ","%s\n",PASSWORD);
send_cmd("SERVER ","%s 1 %s\n",SERVER_NAME,SERVER_DESC);
return 0;
}
return -1; //ooops
}
void services::parseData()
{
char buffer[512];
stringstream convert;
string newbuffer;
for(;;)
{
int index = recv(sock,buffer,512-1,0);
buffer[index]='\0';
newbuffer = buffer;
cout << newbuffer.c_str() << endl;
if(index == 0)
{
cout << "Connection was closed!" << endl;
break;
}
system("pause");
}
}
void services::send_cmd(string cmd)
{
}
void services::send_cmd(string cmd, string param,...)
{
int bufsize;
string newmsg;
newmsg = (cmd += param);
cout << newmsg.c_str() << endl;
bufsize = sizeof(newmsg);
send(sock,newmsg.c_str(),strlen(newmsg.c_str()),0);
}
現在的問題是send_cmd詮釋,他的啓動功能(在連接檢查)實際上是給出% s而不是密碼/服務器信息的值
它只是在控制檯中顯示爲「PASS%s」或「SERVER%s」 - 正如我所說我喜歡%s的概念是否有其他方式來實現這個,或者只是使用send_cmd("PASS",PASSWORD);
代替(它工作正常,我只是挑剔)
send_cmd( 「PASS」, 「%s的\ n」,PASSWORD.c_str()); –
不要忘記send_cmd也發送一個字符串 – aeonicone
將一個字符串(或任何其他非POD類類型的對象)作爲函數的一個變量參數傳遞給未定義的行爲。 –