title: Linked Lists and fscanf
body: <p>I am trying to create a linked list that will store input from a text file. 
  I can get the first set of information but when I come to get succeeding information, I get a seg fault. </p>
  
  <p>Please tell me what I am doing wrong. </p>
  
  <p>Here is my whole code </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct {
      int CourseID;
      char CourseName[30];
   } courseInfo;
  
  struct StudentInfo{
      char StudentID[9];
      char FirstName[20];
      char LastName[25];
      int Courses;
      courseInfo CourseInfo[10];
      struct StudentInfo *next; 
  
  };
  typedef struct StudentInfo studentInfo;
  typedef studentInfo *ListPtr;
  
  studentInfo LoadFile(studentInfo *RootPtr);
  void PrintStruct(studentInfo *startPtr);
  
  int main()
  {
  studentInfo studentList[100];
  ListPtr HeadPtr = NULL;
  printf("Loading File...\n");
  
  LoadFile(HeadPtr);
  printf("Load Success\n");
  PrintStruct(studentList);
  printf("AfterPrint\n");
  
  
  return 0;
  }
  studentInfo LoadFile( studentInfo *RootPtr)
  {
  FILE * ptrFile;
  
  ListPtr NodePtr = malloc(sizeof (studentInfo));
  ListPtr curr;
  int counter, i=0;
  
  
  ptrFile = fopen("studentInfo.txt", "r");
  
  if( ptrFile == NULL)
      printf("Open Unsuccessful\n");
  
  fscanf(ptrFile,"%s", NodePtr-&gt;StudentID);
  printf("%s\n", NodePtr-&gt;StudentID);
  fscanf(ptrFile,"%s", NodePtr-&gt;FirstName);
  printf("%s\n", NodePtr-&gt;FirstName);
  fscanf(ptrFile,"%s", NodePtr-&gt;LastName);
  printf("%s\n", NodePtr-&gt;LastName);
  fscanf(ptrFile,"%d", &amp;(NodePtr-&gt;Courses));
  printf("%d\n", NodePtr-&gt;Courses);
  
  for(counter = 0; counter &lt;= NodePtr-&gt;Courses; counter++)
  {
      fscanf(ptrFile,"%s", NodePtr-&gt;CourseInfo[counter].CourseName);
      printf("%s ", NodePtr-&gt;CourseInfo[counter].CourseName);
      fscanf(ptrFile,"%d", &amp;(NodePtr-&gt;CourseInfo[counter].CourseID));
      printf("%d\n", NodePtr-&gt;CourseInfo[counter].CourseID);
  
  }   
  curr = RootPtr;
  NodePtr-&gt;next = NULL;
  RootPtr = NodePtr;
  NodePtr = NodePtr -&gt; next;
  
  while( strcmp("***", NodePtr-&gt;StudentID) !=0 )
  {
  
      fscanf(ptrFile,"%s", NodePtr-&gt;StudentID);
      printf("%s\n", NodePtr-&gt;StudentID);
  
      fscanf(ptrFile,"%s", NodePtr-&gt;FirstName);
      printf("%s\n", NodePtr-&gt;FirstName);
  
      fscanf(ptrFile,"%s", NodePtr-&gt;LastName);
      printf("%s\n", NodePtr-&gt;LastName);
  
      fscanf(ptrFile,"%d", &amp;(NodePtr-&gt;Courses));
      printf("%d\n", NodePtr-&gt;Courses);
  
          for(counter = 0; counter &lt;= NodePtr-&gt;Courses; counter++)
          {
              fscanf(ptrFile,"%s", NodePtr-&gt;CourseInfo[counter].CourseName);
              printf("%s ", NodePtr-&gt;CourseInfo[counter].CourseName);
              fscanf(ptrFile,"%d",&amp;(NodePtr-&gt;CourseInfo[counter].CourseID));
              printf("%d\n", NodePtr-&gt;CourseInfo[counter].CourseID);
  
          }
      NodePtr = NodePtr-&gt;next;
  }
  fclose(ptrFile);
  
  
  return *NodePtr;
  }
  void PrintStruct(studentInfo *startPtr)
   {
  int i;
  ListPtr begin;
  printf("In print\n");
  for( begin = startPtr; begin-&gt;next != NULL; begin=begin-&gt;next)
  {
      printf("%s\n", begin-&gt;StudentID);
      printf("%s\n", begin-&gt;FirstName);
      printf("%s\n", begin-&gt;LastName);
      printf("%d\n", begin-&gt;Courses);
      for(i = 0; i &lt;= begin-&gt;Courses; i++)
      {   
          printf("%s", begin-&gt;CourseInfo[i].CourseName);
          printf("%d\n", begin-&gt;CourseInfo[i].CourseID);
      }
  
  }
  
  return;
   }
  </code></pre>
  
  <p>`</p>
  
  <p>My text file is : 
  111111111
  Lisa
  Porter
  3
  ENEE 114
  CMSC 412
  ENME 515
  333333333
  Alex
  Simpson
  1
  CMSC 412</p>
  
  <hr>
  
  <p>and will only print up to "333333333"
  followed by a '0' and then a seg fault. </p>