我真的很喜欢 %s,ID 的概念。我注意到这适用于 char,并且我知道这是 C 风格的功能。我想知道是否还有其他方法可以用字符串实现 %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 int 他的启动功能(在连接检查中)实际上给出了 %s 而不是密码/服务器信息的值
它只是在控制台中显示为“PASS %s”或“SERVER %s”——正如我所说,我喜欢 %s 的概念是否有其他方法可以实现这一点,或者只是使用send_cmd("PASS",PASSWORD);
它(效果很好,我只是挑剔)