title: C - Parse file and write to separate files based on the last field
body: <p>The software intends to read from a csv styled file (it's delimited by space not by a comma) and split the initial file into two new ones. The two files are determined by the last field, which is a binary value. As it stands, it currently reads the file character by character. I want it to recognize the space, and only run the individual character check on the very last field. To my understanding, strtok() will come in handy, but I'm struggling to find a way to incorporate that into the existing software.</p>
  <p>Any help would be greatly appreciated :)</p>
  <pre><code>/*
   * C program to parse a file, and split it into two based on the final line of input
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  
  /* Function declarations */
  int isMale(signed char _line);
  int isFemale(signed char _line);
  
  
  int isMale(signed char _line)
  {
  
  }
  
  int isFemale(signed char _line)
  {
  
  }
  
  
  
  int main()
  {
      /* File pointer to hold reference to different files */
  
      FILE * fPtrIn,      // Input file
           * fPtrMale,    // Males of school age 
           * fPtrFemale,  // Females of school age
           * fPtrMisc;    // Data not within the given parameters
      
      // current_char is the current character being read
      // success stores the read status
      char current_char;
      int success;
  
      // Open all files to perform read/write.
      fPtrIn       = fopen(&quot;data/example.txt&quot;, &quot;r&quot;);
      fPtrMale     = fopen(&quot;data/males.txt&quot; , &quot;w&quot;);
      fPtrFemale   = fopen(&quot;data/females.txt&quot;  , &quot;w&quot;);
      fPtrMisc     = fopen(&quot;data/erroneus.txt&quot;, &quot;w&quot;);
  
  
      // fopen() return NULL if unable to open file in given mode. 
      if(fPtrIn == NULL || fPtrMale == NULL || fPtrFemale == NULL || fPtrMisc == NULL)
      {
          // Unable to open file, exit software 
          printf(&quot;Unable to open file.\n&quot;);
          printf(&quot;Please check whether file exists and you have read/write privilege.\n&quot;);
          exit(EXIT_FAILURE);
      }
  
  
      // File open success message
      printf(&quot;File opened successfully. \n\n&quot;);
  
      // Read an integer and store read status in success.
      while (fscanf(fPtrIn, &quot;%d&quot;, &amp;current_char) != -1)
      {
          // Write each one to separate file
          if (isMale(current_char))
              fprintf(fPtMale, &quot;%d\n&quot;, current_char);
          else if (isFemale(current_char))
              fprintf(fPtrFemale, &quot;%d\n&quot;, current_char);
          else
              fprintf(fPtrMisc, &quot;%d\n&quot;, current_char);
  
      }
  
  
      // Done with all files, hence close all.
      fclose(fPtrIn);
      fclose(fPtrMale);
      fclose(fPtrFemale);
      fclose(fPtrMisc);
  
      printf(&quot;Data written to files successfully.&quot;);
  
  
      return 0;
  }
  
  </code></pre>