我有一个程序应从控制台读取命令,并根据命令执行以下几种操作之一。这是我到目前为止的内容:
void ConwayView::listening_commands() {
string command;
do {
cin >> command;
if ("tick" == command)
{
// to do
}
else if ("start" == command)
{
// to do for start
}
...
} while (EXIT != command);
}
Run Code Online (Sandbox Code Playgroud)
如果存在大量命令switch,则使用代替if语句会有所帮助。您建议使用什么模式来提供交互式命令行?
我正在尝试获取A,MX和NS A服务器记录,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>
#include <netdb.h>
#define N 4096
int main (int argc, char *argv[])
{
u_char nsbuf[N];
char dispbuf[N];
ns_msg msg;
ns_rr rr;
int i, l;
if (argc < 2) {
printf ("Usage: %s <domain>\n", argv[0]);
exit (1);
}
// HEADER
printf("Domain : %s\n", argv[1]);
// ------
// A RECORD
printf("A records : \n");
l = res_query(argv[1], ns_c_any, ns_t_a, nsbuf, sizeof(nsbuf));
if (l < 0)
{
perror(argv[1]);
}
ns_initparse(nsbuf, l, &msg); …Run Code Online (Sandbox Code Playgroud)