title: C: Segmentation fault with printing Link List
body: <p>I'm pretty new to C, but I wanted to build a program that could allows users store ip addresses obtained from a traceroute/tracert that is first stored into a text file. It then allows them to print the next/previous hop. I used a linked list, but the printing gives me a segmentation error.</p>
  
  <p>I tried looking through it but I can't find any error, would someone point out my error and guide me? Thanks in advance!!</p>
  
  <p>(Sorry for the poor indentation!!)</p>
  
  <pre><code># include &lt;stdio.h&gt;
  # include &lt;string.h&gt;
  # include &lt;stdlib.h&gt;
  
  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 a network node\n");
    printf("2. Identify the next hops of a network node\n");
    printf("3. Quit\n");
  }
  
  int getInput()
  {
    int choice;
    printf("Please select your choice (1 to 3) =&gt;: ");
    scanf("%d", &amp;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-&gt;IP1 = ip1;
    newNode-&gt;IP2 = ip2;
    newNode-&gt;IP3 = ip3;
    newNode-&gt;IP4 = ip4;
    newNode-&gt;next = 0;            // NULL Pointer
    newNode-&gt;prev = 0;            // NULL Pointer
    return newNode;
  }
  
  void addToBack(Node * tempnode)
  {
    Node *n = Topology;
    Node *tail = 0;
    while (n != NULL) {
      tail = n;
      n = n-&gt;next;
    }
    tail-&gt;next = tempnode;
    tempnode-&gt;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 is an error with opening this file\n");
    } else {
      while (fgets(data, 256, newFile) != NULL) {
  
        if (linecount != 3) {
          linecount++;
          continue;
        } else {
  
          if (linecount == 3 &amp;&amp; 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, &amp;ip1, &amp;ip2x, &amp;ip3, &amp;ip4);
  
            if ((ip[0] &lt;= 'z' &amp;&amp; ip[0] &gt;= 'a')
                || (ip[0] &lt;= 'Z' &amp;&amp; ip[0] &gt;= 'A')) {
              sscanf(data, "%s %s %s %s %s %s %s %s %s",
                     nth1, nth2, nth3, nth4, nth5, nth6, nth7, ip, ip2);
              //Rescanning for anomaly results with additional hostname
              strncpy(ip2new, ip2 + 1, strlen(ip2) - 2);
              ip2new[strlen(ip2) - 2] = '\0';
  
              int i;
              char *temp;
              char *ipcmp[4];
              i = 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 &amp;&amp; data[2] == '\0') {
            linecount = -2;
            printf("\n");
          }
        }
      }
    }
  }
  
  void printNodes()
  {
    Node *n = Topology;
    while (n != 0) {
      printf("The node is %d.%d.%d.%d\n", n-&gt;IP1, n-&gt;IP2, n-&gt;IP3, n-&gt;IP4);
      n = n-&gt;next;                // Jump to 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 the ip addresses
    do {
      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 a valid choice (1-3) \n");
        fail = 0;
        break;
      }
    } while (fail != 1);
    while (Topology != 0) {
      free(Topology);
      Topology = Topology-&gt;next;
    }
  }
  </code></pre>