Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
涉及C中结构的ip地址程序的问题_C - Fatal编程技术网

涉及C中结构的ip地址程序的问题

涉及C中结构的ip地址程序的问题,c,C,我正在学习C语言中的结构,我编写了一个程序,该程序可以读取多达100个地址和昵称的列表,并打印一个消息列表,该列表标识来自同一位置的每对计算机,即地址的前两个组件中具有匹配值的每对计算机。由于某些原因,我的程序总是打印少于5项的内容。我似乎不知道我做错了什么。感谢您的帮助 C代码: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct { unsigned int

我正在学习C语言中的结构,我编写了一个程序,该程序可以读取多达100个地址和昵称的列表,并打印一个消息列表,该列表标识来自同一位置的每对计算机,即地址的前两个组件中具有匹配值的每对计算机。由于某些原因,我的程序总是打印少于5项的内容。我似乎不知道我做错了什么。感谢您的帮助

C代码:

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

typedef struct {
    unsigned int xx,yy,zz,mm;
    char name[10];
}address_t;

int scan_address(address_t add[]);
void print_address(address_t);
int local_address(address_t, address_t);

int main(void)
{
    address_t adds[100];
    int count=0, res=0, i, j;

    count = scan_address(adds);

    for(i=0; i<count; i++)
    {
        for(j=i+1; j<count; j++)
        {
            res = local_address(adds[i], adds[j]);
            if(res)
            {
                printf("\nMachines %s and %s are on the same local network\n", adds[i].name, adds[j].name);
                print_address(adds[i]);
                print_address(adds[j]);
            }
        }
    }
return(0);
}

int scan_address(address_t adds[100])
{
    int status=1, i=0;
    FILE *info;

    info = fopen("ipaddresses.txt", "r");

    if(info==NULL)
    printf("\nipaddresses.txt does not exist. Try again\n");
    else{
        while(status>0)
        {
            status=fscanf(info, "%d %d %d %d %s", &adds[i].xx, &adds[i].yy, &adds[i].zz, &adds[i].mm, adds[i].name);
            if(status==5)
            {
                i++;
            }else if(status<5 && status>0){
                //This is the issue. This message is being printed for some reason
                printf("\nInvalid input, the total number of items is less than 5\n"); 
            }
            if(adds[i].xx==0 && adds[i].yy==0 && adds[i].zz==0 && adds[i].mm==0)
            break;
        }
    }
    if(i>0)
    printf("\nipaddress.txt was succesfully imported with %d record\n", i);
    fclose(info);
    return(i-1);
}

//Function that returns True if the addresses are on the same local network
int local_address(address_t add1, address_t add2)
{
    int result=0;
    if(add1.xx == add2.xx && add1.yy == add2.yy)
    {
        result = 1;
    }
    return(result);
}

void print_address(address_t add)
{
    printf("%d %d %d %d\t %s\n", add.xx, add.yy, add.zz, add.mm, add.name);
}

请务必阅读@melpomene与Eric Lippert关于调试小程序的优秀文章的链接


你有两个问题。首先,您的
scanf()
格式字符串不允许IP地址八位字节之间的小数。文件中出现意外的分隔符(小数)会导致第一个分隔符之后的所有扫描转换失败,因此它返回1,触发您看到的打印。将格式字符串更改为
“%d.%d.%d.%d%s”

第二个是在
scan_address()
中,在测试终止行之前,您正在递增
i
。换句话说,当此检查运行时:

if(adds[i].xx==0 && adds[i].yy==0 && adds[i].zz==0 && adds[i].mm==0)
您已经增加了
i
以指向下一条记录,并且您正在测试未初始化的内存。这实际上是未定义的行为,但很可能“偶然”此内存包含零,从而触发输入检查的结束。在递增
i
之前,可以将
if
(和
中断
)向上移动到
if(status==5)
代码块中


通过这些变化,我得到:

ipaddress.txt was succesfully imported with 8 record

Machines green and red are on the same local network
111 22 3 44  green
111 22 6 77  red

Machines blue and yellow are on the same local network
222 33 4 55  blue
222 33 8 99  yellow

Machines cyan and black are on the same local network
333 44 1 22  cyan
333 44 2 33  black

请尝试“%d.%d.%d.%d”。Scanf需要知道您期望的数据的实际格式。我阅读了这篇文章并将进行调试。到目前为止,这一切都很顺利。非常感谢。
ipaddress.txt was succesfully imported with 8 record

Machines green and red are on the same local network
111 22 3 44  green
111 22 6 77  red

Machines blue and yellow are on the same local network
222 33 4 55  blue
222 33 8 99  yellow

Machines cyan and black are on the same local network
333 44 1 22  cyan
333 44 2 33  black