title: Sorting an array from a text file
body: <p>I can' seem to sort a text file I have in ascending order. For some reason it prints my shuffle array with the first and second entry swapped. I seem to have confused myself after hours of trying to get it to work and may have made a few mistakes.  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  
  // Accepts: command line input
  // Returns: 0 if no error
  
  int main(int num_args, char *arg_strings[])
  {
      int x = 0, i, track_count = 0;
      unsigned long Max_Length = 0;
      char line[500], *temp;
      FILE *file = fopen("playlist.txt", "r");
      /* The next line checks if the playlist file exists and if it's not there, "Cannot Open File" is printed to the screen */
      if (file == NULL)
      {
          printf("Cannot open file\n");
      }
      /* The following code identifies each line in the text and lines are shuffled accordingly */
  
      while (fgets(line, sizeof(line), file) != NULL)
      {
          track_count++;
          if (strlen(line) &gt; Max_Length)
              Max_Length = strlen(line);
      }
      rewind(file);
      char *Array[track_count];
      while (fgets(line, sizeof(line), file) != NULL)
      {
          Array[x] = malloc(strlen(line));
          if (Array[x] == NULL)
          {
              printf("A memory error occurred.\n");
              return(1);
          }
          strcpy(Array[x], line);
          /* change \n to \0 */
          Array[x][strlen(Array[x]) - 1] = '\0';
          x++;
      }
  
      printf("The original playlist is:\n");
      for (x = 0; x &lt; track_count; x++)
          printf("%2d %s\n", x, Array[x]);
      /*  The array will now be shuffled: */
      srand((unsigned int)time(NULL));
      for (x = track_count - 2; x &gt; 1; x--)
      {
          while (1)
          {
              i = rand() % (track_count - 1) + 1;
              if (Array[x + 1][0] == Array[i][0])
                  continue;
              if (Array[x - 1][0] == Array[i][0])
                  continue;
              if (Array[i + 1][0] == Array[x][0])
                  continue;
              if (Array[i - 1][0] == Array[x][0])
                  continue;
  
              temp = Array[x];
              Array[x] = Array[i];
              Array[i] = temp;
              break;
          }
      }
  
      printf("\nShuffled Array\n");
      for (x = 0; x &lt; track_count; x++)
          printf("%2d %s\n", x, Array[x]);
  
      /* Sorting */
  
      int m = 0;
      int z = 0;
      int k = 0;
      char j = 0;
      char tempArtist[Max_Length][Max_Length];
  
      for (m = 0; m &lt; track_count; m++)
      {
          for (z = 0; z &lt;track_count - 1 - m; z++)
          {
              if (strcmp(Array[j], Array[j + 1]) &gt; 0)
              {
                  strcpy(tempArtist, Array[j]);
                  strcpy(Array[j], Array[j + 1]);
                  strcpy(Array[j + 1], tempArtist);
              }
          }
      }
      puts("");
      printf("Sorted Playlist:");
      for (k = 0; k &lt;= track_count; k++)
      {
          printf("\n%s", Array[k]);
      }
  
      return 0;
  }
  </code></pre>