C: Segmentation fault with printing Link List -
i'm pretty new c, wanted build program allows users store ip addresses obtained traceroute/tracert first stored text file. allows them print next/previous hop. used linked list, printing gives me segmentation error.
i tried looking through can't find error, point out error , guide me? in advance!!
(sorry poor indentation!!)
# include <stdio.h> # include <string.h> # include <stdlib.h> int id = 0; int list = 0; int nodes = 0; file *readfile(char *filename) { file *rawdata = fopen(filename, "r"); return rawdata; } void printmenu() { printf(" ========== menu =============\n\n"); printf("1. report id of previous hops of network node\n"); printf("2. identify next hops of network node\n"); printf("3. quit\n"); } int getinput() { int choice; printf("please select choice (1 3) =>: "); scanf("%d", &choice); return choice; } struct nodeinfo { int id; int ip1; int ip2; int ip3; int ip4; struct nodeinfo *next; struct nodeinfo *prev; }; typedef struct nodeinfo node; node *topology; node *createnode(int ip1, int ip2, int ip3, int ip4) { node *newnode = malloc(sizeof(node)); newnode->ip1 = ip1; newnode->ip2 = ip2; newnode->ip3 = ip3; newnode->ip4 = ip4; newnode->next = 0; // null pointer newnode->prev = 0; // null pointer return newnode; } void addtoback(node * tempnode) { node *n = topology; node *tail = 0; while (n != null) { tail = n; n = n->next; } tail->next = tempnode; tempnode->prev = tail; } void printfile(file * newfile) { char data[256], nth1[50], nth2[50], nth3[50], nth4[50], nth5[50], nth6[50], nth7[50], ip[50], ip2[15], ip2new[14]; int linecount = -1, strlength; int ip1, ip2x, ip3, ip4; int ip11, ip21, ip31, ip41; if (newfile == null) { printf("there error opening file\n"); } else { while (fgets(data, 256, newfile) != null) { if (linecount != 3) { linecount++; continue; } else { if (linecount == 3 && data[2] != '\0') { sscanf(data, "%s %s %s %s %s %s %s %s", nth1, nth2, nth3, nth4, nth5, nth6, nth7, ip); sscanf(data, "%s %s %s %s %s %s %s %d.%d.%d.%d", nth1, nth2, nth3, nth4, nth5, nth6, nth7, &ip1, &ip2x, &ip3, &ip4); if ((ip[0] <= 'z' && ip[0] >= 'a') || (ip[0] <= 'z' && ip[0] >= 'a')) { sscanf(data, "%s %s %s %s %s %s %s %s %s", nth1, nth2, nth3, nth4, nth5, nth6, nth7, ip, ip2); //rescanning anomaly results additional hostname strncpy(ip2new, ip2 + 1, strlen(ip2) - 2); ip2new[strlen(ip2) - 2] = '\0'; int i; char *temp; char *ipcmp[4]; = 0; temp = strtok(ip2new, "."); while (temp != null) { ipcmp[i++] = temp; temp = strtok(null, "."); } node *tempnode = createnode(ip2new); if (topology != 0) { addtoback(tempnode); } else { topology = tempnode; } } else { printf("%s\n", ip); printf("%d.%d.%d.%d\n", ip1, ip2x, ip3, ip4); node *tempnode2 = createnode(ip); if (topology != 0) { addtoback(tempnode2); } else { topology = tempnode2; } continue; } } if (linecount == 3 && data[2] == '\0') { linecount = -2; printf("\n"); } } } } } void printnodes() { node *n = topology; while (n != 0) { printf("the node %d.%d.%d.%d\n", n->ip1, n->ip2, n->ip3, n->ip4); n = n->next; // jump next node } } int main(int argc, char *argv[]) { int option, fail; file *filedata; char *file; file = argv[1]; filedata = readfile(file); //open file printfile(filedata); //prints ip addresses { printmenu(); option = getinput(); switch (option) { case 1: printf("you have selected 1\n\n"); fail = 0; printnodes(); break; case 2: printf("you have selected 2\n\n"); fail = 0; break; case 3: fail = 1; break; default: printf("please enter valid choice (1-3) \n"); fail = 0; break; } } while (fail != 1); while (topology != 0) { free(topology); topology = topology->next; } }
your create node method have 4 parameters:
node *createnode(int ip1, int ip2, int ip3, int ip4)
but invoke method passing 1 parameter:
node *tempnode = createnode(ip2new); node *tempnode2 = createnode(ip);
also pass arrays when method accept integers.
this @ least 2 sources of errors in code.
Comments
Post a Comment