简体   繁体   中英

Issues with ip address program involving structures in C

I am learning about structures in C and I have written a program that is supposed to read in a list of up to 100 addresses and nicknames and print a list of messages identifying each pair of computers from the same locality, that is, each pair of computers with matching values in the first two components of the address. For some reason, my program will always print that I have less than 5 items. I can't seem to figure out what I did wrong. Any help is appreciated.

C code:

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

Text file:

111.22.3.44 green
222.33.4.55 blue
111.22.6.77 red
222.33.8.99 yellow
333.44.1.22 cyan
444.55.1.22 purple
333.44.2.33 black
444.55.4.55 white
0.0.0.0 none

Please do read @melpomene's link to Eric Lippert's excellent article on debugging small programs.


You have two issues. The first is that your scanf() format string does not allow for the decimals between IP address octets. The presence in the file of unexpected delimiters (the decimals) causes all the scan conversions after the first to fail, so it returns 1, triggering the print you're seeing. Change the format string to "%d.%d.%d.%d %s" .

The second is that in scan_address() you're incrementing i before you test for the terminating line. In other words, when this check runs:

if(adds[i].xx==0 && adds[i].yy==0 && adds[i].zz==0 && adds[i].mm==0)

You've already incremented i to point to the next record, and you're testing uninitialized memory. This is actually undefined behavior, but it's likely that "by chance" this memory contains zeroes, triggering the end-of-input check. You can move the if (and the break ) up into the if (status == 5) code block before incrementing i .


With these changes I get:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM