C 中处理文本文件的异常行为

发布于 2024-10-05 18:58:56 字数 1572 浏览 0 评论 0原文

我正在尝试模拟 DNS 服务器的行为,我有一个名为 hosts.txt 的数据库,其中包含 machine_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

抱歉,如果问题相当愚蠢,我是这种语言的新手..谢谢大家的帮助和请原谅我的英语

I'm trying to simulate the behavior of a DNS server, which I have a DB named hosts.txt containing machine_names/IP_addresses, for example:

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

Here's my code:

#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;
}

My code reads from standard input, the value of the machine and indicate the IP address that is assigned to that machine on the server. The problem is that the program is not normal behavior, I do not know how to modify my code to an expected output, eg

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

Sorry if the question is rather silly, I'm new to this language .. thank you all for your help and excuse my English

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮倦 2024-10-12 18:58:56

您要做的就是:

将文件中的字符串读入 tmp

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

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

请注意,machineip_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 正文的一部分,以便仅在找到时才打印它们比赛。
目前,即使未找到匹配项,您也会进行打印。在这种情况下,您将打印垃圾,因为您的指针 serverip_add 尚未初始化

This is what you do:

Read a string from the file into tmp.

Check if user entered string par is present in tmp.

If yes you go ahead and tokenize tmp on / and make the pointer machine point to the first piece and ip_add point to the next piece.

Note that machine and ip_add are just pointers. And they point at various indices of tmp. So later when you continue with the loop you read the new string again into tmp, overwriting it. This causes the problem and your pointer are now pointing to a changed string.

To avoid this just add a break after a successful match:

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

Also your final printf:

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

should be part of your if body, so that you print them only if you've found the match.
Currently you are printing even if a match is not found. In which case you'll be printing junk as your pointers server and ip_add have not been initialized.

旧话新听 2024-10-12 18:58:56

看来您的问题可能出在 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 变量在函数开始时未初始化,这不会给出意外(且未定义)的结果。

It looks like your problem might be with the flag variable. Using a string for a flag value seems very unusual. Try this:

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

In your code above, the flag variable is uninitialised at the start of the function which won't may give unexpected (and undefined) results.

零度℉ 2024-10-12 18:58:56

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

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

#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;
}

Below is the fixed code. There is several minor errors, mostly missing inits. Think what would happen if machine is not found.

The main one is that you use the same buffer for each iteration of the loop, thus clearing up the previously found server. I fixed that by exiting loop. Another method could be to copy the found result in another buffer.

#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;
}
停顿的约定 2024-10-12 18:58:56
if(strcmp(flag, "1") == 0)

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

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

if(strcmp(flag, "1") == 0)

you can't check this because if flag wasn't initializied it's undefined behavior

strcmp() searchs for '\0' character in both arguments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文