Review the structure type address_t described in Programming Project 5 of Chapte
ID: 3714459 • Letter: R
Question
Review the structure type address_t described in Programming Project 5 of Chapter 10. Write a program that will create a binary file of address_t structures by repeatedly prompting the user to enter the IP(internet protocol) address and nickname and writing the address_t structure to the binary file. Create a second program that reads each address_t structure from the binary file and displays it in a readable format on the screen.//////////Here is problem 5 but the part in bold is what needs to be solved///////////// Programming Project 5: Numeric addresses for computers on the international network Internet are composed of four parts, separated by periods, of the form:
xx.yy.zz.mm
where xx, yy, zz, and mm are positive integers. Locally, computers are usually known by a nickname as well. You are designing a program to process a list of Internet addresses, identifying all pairs of computers from the same locality. Create a structure type called address_t with components for the four integers of an Internet address and a fifth component in which to store an associated nickname of 10 characters. Your program should read a list of up to 100 addresses and nicknames terminated by a sentinel address of all zeros and a sentinel nickname.
SAMPLE DATA:
111.22.3.44 platte
555.66.7.88 wabash
111.22.5.66 green
0.0.0.0 none
The program should display 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. In the messages, the computers should be identified by their nicknames.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
struct address_t {
int part1;
int part2;
int part3;
int part4;
char nickname[11]; //10 plus 1 for null
}address[15];
void scan_address(char[],struct address_t[],int );
void print_address(struct address_t);
int getnum(int*,char[],char);
int local_address(struct address_t,struct address_t);
int main()
{FILE *input;
char buffer[30],filename[15];
int i=0,j,k;
printf("what is the name of the file you are using? ");
scanf("%s",&filename);
input = fopen(filename,"r");
if(input == NULL)
{ printf("Error opening input file! ");
return 0;
}
printf(" The addresses are address nickname ");
for(i=0;;i++)
{fscanf(input,"%s",&buffer);
scan_address(buffer,address,i);
if(address[i].part1==0&&address[i].part2==0&&address[i].part3==0
&&address[i].part4==0)
break;
fscanf(input,"%s",&address[i].nickname);
print_address(address[i]);
}
fclose(input);
printf(" ");
for(j=0;j<i-1;j++)
for(k=j+1;k<i;k++)
if(local_address(address[j],address[k])==1)
printf("Machines %s and %s are on the same local network ",
address[j].nickname,address[k].nickname);
getch();
return 0;
}
int local_address(struct address_t a,struct address_t b)
{if(a.part2==b.part2)
return 1;
else return 0;
}
int getnum(int *i,char b[],char c)
{int val=0,j;
while(b[*i]!=c)
{val=val*10+(b[*i]-'0');
*i=*i+1;
}
*i=*i+1;
return val;
}
void scan_address(char b[],struct address_t a[],int n)
{int i=0,val;
a[n].part1=getnum(&i,b,'.');
a[n].part2=getnum(&i,b,'.');
a[n].part3=getnum(&i,b,'.');
a[n].part4=getnum(&i,b,'');
}
void print_address(struct address_t address)
{printf("%d.%d.%d.%d %s ",address.part1,address.part2,address.part3,
address.part4,address.nickname);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.