#include <stdio.h>
#include <string.h>
#define MAX_input_size 100
struct RoutingInfo{ /* Structure to hold the IP entrys */
unsigned int IPDestinationNetwork;
unsigned int subnetMask;
int prefix;
unsigned int gateway;
char* interface;
} entry[MAX_input_size];
int computePrefix (unsigned long decimSM){ /* computes the prefix of the Subnet Mask */
int prefix = 0;
while (decimSM){
prefix += decimSM % 2;
decimSM = decimSM / 2;
}
return prefix;
}
int main (int argc, char *argv[]){
FILE *fpin;
int i = 0;
char buffer[50];
if (argc < 2){ /* Checking for arguments */
puts ("usage: ./a.out input.txt\n");
return 1;
}
fpin = fopen (argv[1], "r"); /* Opening the file for reading*/
if (fpin == NULL){/* File opening error */
puts ("cannot open file\n");
return 1;
}
/* File opened */
while (fgets (buffer, 50, fpin) != NULL){ /* Reading input entrys in to the structure */
entry[i].IPDestinationNetwork = inet_addr (strtok (buffer, "\t"));
entry[i].subnetMask = inet_addr (strtok (NULL, "\t"));
entry[i].prefix = computePrefix (entry[i].subnetMask);
entry[i].gateway = inet_addr (strtok (NULL, "\t"));
entry[i].interface = strtok (NULL, "\t");
printf ("%u\t%u\t%d\t%u\t%s\n", entry[i].IPDestinationNetwork, entry[i].subnetMask, entry[i].prefix, entry[i].gateway, entry[i].interface);
i++;
printf ("---------------------%s\n", entry[0].interface);
}
fclose (fpin);
return 0;
}
input file: input.txt
\t means there is a tab space in between
194.24.0.0 \t255.255.248.0 \t194.24.12.0 \tRouter_4
194.24.8.0 \t255.255.252.0 \t127.0.0.1 \tRouter_1
194.24.12.0 \t255.255.252.0 \t194.24.8.0 \tRouter_3
194.24.16.0 \t255.255.240.0 \t194.24.0.0 \tRouter_2
194.24.8.0 \t255.255.254.0 \t194.24.12.0 \tRouter_4
194.24.16.0 \t255.255.248.0 \t127.0.0.1 \tRouter_1
in the output for the above code, why the statement
printf ("---------------------%s\n", entry[0].interface);
is printing different values of entry[0].interface on successive iterations.
can any one please help me on this problem.
Thank you in advance.