title: Reading a text file into 2 separate arrays of characters (in C)
body: <p>For a class I have to write a program to read in a text file in the format of:</p>
  
  <hr>
  
  <p>T A E D Q Q<br>
   Z H P N I U<br>
   C K E W D I<br>
   V U X O F C<br>
   B P I R G K<br>
   N R T B R B<br>
   EXIT<br>
   THE<br>
   QUICK<br>
   BROWN<br>
   FOX</p>
  
  <hr>
  
  <p>I'm trying to get the characters into an array of chars, each line being its own array.
  I'm able to read from the file okay, and this is the code I use to parse the file:</p>
  
  <hr>
  
  <pre><code>char** getLinesInFile(char *filepath)  
  {  
      FILE *file;  
      const char mode = 'r';  
      file = fopen(filepath, &amp;mode);  
      char **textInFile;  
  
      /* Reads the number of lines in the file. */
      int numLines = 0;
      char charRead = fgetc(file);
      while (charRead != EOF)
      {
          if(charRead == '\n' || charRead == '\r')
          {
              numLines++;
          }
          charRead = fgetc(file);
      }
  
      fseek(file, 0L, SEEK_SET);
      textInFile = (char**) malloc(sizeof(char*) * numLines);
  
      /* Sizes the array of text lines. */
      int line = 0;
      int numChars = 1;
      charRead = fgetc(file);
      while (charRead != EOF)
      {
          if(charRead == '\n' || charRead == '\r')
          {
              textInFile[line] = (char*) malloc(sizeof(char) * numChars);
              line++;
              numChars = 0;
          }
          else if(charRead != ' ')
          {
              numChars++;
          }
          charRead = fgetc(file);
      }
  
      /* Fill the array with the characters */
      fseek(file, 0L, SEEK_SET);
      charRead = fgetc(file);
      line = 0;
      int charNumber = 0;
      while (charRead != EOF)
      {
          if(charRead == '\n' || charRead == '\r')
          {
              line++;
              charNumber = 0;
          }
          else if(charRead != ' ')
          {
              textInFile[line][charNumber] = charRead;
              charNumber++;
          }
          charRead = fgetc(file);
      }
  
      return textInFile;
  }
  </code></pre>
  
  <hr>
  
  <p>This is a run of my program:</p>
  
  <hr>
  
  <p>Welcome to Word search!</p>
  
  <p>Enter the file you would like us to parse:testFile.txt
  TAEDQQ!ZHPNIU!CKEWDI!VUXOFC!BPIRGK!NRTBRB!EXIT!THE!QUICK!BROWN!FOX
  Segmentation fault</p>
  
  <hr>
  
  <p>What's going on? A), why are the exclamation marks there, and B) why do I get a seg fault at the end? The last thing I do in the main is iterate through the array/pointers.</p>