1

我正在尝试模拟 DNS 服务器的行为,我有一个名为 contains 的数据库hosts.txtmachine_names/IP_addresses例如:

equipo_00/169.0.1.169
sala_oeste_01/200.1.2.200
sala_oeste_02/200.2.3.200
sala_oeste_03/200.3.4.200
MEMFIS_04/201.4.5.201
ICARO_05/200.5.6.200
equipo_06/169.6.7.169
sala_este_07/201.7.8.201
sala_este_08/201.8.9.201
CEC_09/189.9.10.189

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    char* machine, *ip_add;
    FILE* db;
    char* flag;
    char  tmp[256];
    char  par[256];
    printf("> ");
    scanf("%s", par);
    db = fopen("hosts.txt", "r");
    while(db != NULL && fgets(tmp, sizeof(tmp), db) != NULL)
    {
        if(strstr(tmp, par))
        {
            flag = "1"; // flag setting to 1 means find the line
            machine = strtok(tmp, "/");//here's supposed to get the value of the machine
            ip_add = strtok(NULL, "/");//here's supposed to get the value of the ip adress
        }
    }//while

    if(strcmp(flag, "1") == 0) //
    {
        printf("here\n");
    }
    else
    {
        flag = "0"; //flag setting to 0
        printf("there\n");
    }

    printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add);
    return 0;
}

我的代码从标准输入读取机器的值并指示分配给服务器上该机器的 IP 地址。问题是程序行为不正常,我不知道如何将我的代码修改为预期的输出,例如

input: equipo_00
output: flag = 1 pc = equipo_00 server=169.0.1.169

对不起,如果这个问题很愚蠢,我是这门语言的新手.. 谢谢大家的帮助,请原谅我的英语

4

4 回答 4

4

这是你做的:

将文件中的字符串读入tmp.

检查用户输入的字符串par是否存在于tmp.

如果是,您继续标记化tmp并使/指针machine指向第一块并ip_add指向下一块。

请注意,machineandip_add只是指针。他们指向 的各种指数tmp。因此,稍后当您继续循环时,您再次将新字符串读入 tmp,并覆盖它。这会导致问题,您的指针现在指向已更改的字符串。

为了避免这种情况,只需在成功匹配后添加一个中断:

if (strstr(tmp, par)) {
   flag = "1";
   machine = strtok(tmp, "/");
   ip_add = strtok(NULL, "/");
   break;       
}

还有你的决赛printf

printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add);

应该是您if身体的一部分,因此只有在找到匹配项时才打印它们。当前,即使未找到匹配项,您也正在打印。在这种情况下,您将打印垃圾作为指针server并且ip_add尚未初始化

于 2010-12-03T07:44:08.380 回答
0

看起来您的问题可能与flag变量有关。使用字符串作为标志值似乎很不寻常。试试这个:

int flag = 0; // always initialise your variables

if (strstr(tmp, par) == 0) {
    flag = 1; // just an integer value
}

if (flag) { // easier to test the value too!
    // ...
}

printf("flag=%d\n", flag); // ints use %d format value

在上面的代码中,flag变量在函数开始时未初始化,这可能不会产生意外(和未定义)的结果。

于 2010-12-03T07:35:06.327 回答
0

下面是固定代码。有几个小错误,主要是缺少初始化。想想如果找不到机器会发生什么。

主要的一个是您为循环的每次迭代使用相同的缓冲区,从而清除先前找到的服务器。我通过退出循环解决了这个问题。另一种方法可能是将找到的结果复制到另一个缓冲区中。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    char* machine = "";
    char* ip_add = "";
    FILE* db;
    char* flag;
    char  tmp[256];
    char  par[256];
    printf("> ");
    scanf("%s", par);
    db = fopen("hosts.txt", "r");
    while(db != NULL && fgets(tmp, sizeof(tmp), db) != NULL)
    {
        char * found = strstr(tmp, par);
        if(found)
        {
            flag = "1"; // flag setting to 1 means find the line
            machine = strtok(found, "/");//here's supposed to get the value of the machine
            ip_add = strtok(NULL, "/");//here's supposed to get the value of the ip adress
            break;
        }
    }//while

    if(strcmp(flag, "1") == 0)
    {
        flag = "0"; //flag setting to 0
    }

    printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add);
    return 0;
}
于 2010-12-03T08:02:17.550 回答
0
if(strcmp(flag, "1") == 0)

你不能检查这个,因为如果标志没有初始化它是未定义的行为

strcmp() 在两个参数中搜索 '\0' 字符

于 2010-12-03T09:14:56.483 回答