title: Issues with 'saving progress' of function
body: <p>I'm trying to parse through some CSV log files extracting only the n'th field (dismissing the others for speed). My function works as expected when I use a buffer size with fread greater than the size of the input.</p>
  
  <p>The problem is when I read in part of the input and try to continue where I left off the next time the function is called. I believe the problem lies in how I'm handling the null terminator and setting my globals, but I just can't seem to figure it out.</p>
  
  <p>Any help with understanding what I'm doing wrong greatly appreciated!</p>
  
  <p>Code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  int gcomc = 0;
  int gpos = 0;
  
  void test(char *str, int len)
  {
      const char *ptr = str;
            char ch;
            int i;
            char so[10];
            int comc = gcomc;
            int pos = gpos;
  
      for(i = 0; i &lt; len; i++)
      {
          ch = ptr[i];
  
          switch(ch) 
          {
               case ';':
                   comc++;
                   break;
  
               case '\0':
                   gcomc = comc;
                   gpos = pos;
                   break;
  
               default:
                   if (comc == 3) {
                       ch = ptr[i];
                       so[pos++] = ch;
                   }
                   if (comc == 7) {
                       printf(" %s ", so);
                       comc = 0;
                       pos = 0;
                       gcomc = 0;
                       gpos = 0;
                   }
          }
      }
  
  return;
  }
  
  int main(int argc, char* argv[]) 
  {
  
  FILE *fin=fopen("test.txt", "rb");
      char buffer[100 + 1];
      size_t bsz;
  
      while((bsz = fread(buffer, sizeof *buffer, 100, fin)) &gt; 0)
      {
          buffer[bsz] = '\0';
          test(buffer, bsz);
      }
  
  return 1;
  }
  </code></pre>
  
  <p>Input</p>
  
  <pre><code>A;B;C;D;E;F;G;H
  I;J;K;L;M;N;O;P
  Q;R;S;T;U;V;W;X
  Y;Z;1;2;3;4;5;6
  </code></pre>
  
  <p>Output with buffer size of 100 (101)</p>
  
  <pre><code> D  L  T  2 
  </code></pre>
  
  <p>Output with buffer size of 10 (11)</p>
  
  <pre><code> D  P
  Q  X
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>Edit:
  Thank you for the comments and code, I've reworked my (rather dumb written) code - any further criticism is welcome (constructive or destructive, I learn from it all):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  void test(char *str, int len);
  
  int gcomc, gpos = 0;
  
  void test(char *str, int len)
  {
  const char *ptr = str;
            char ch;
            int i;
            static char so[10];
  
      for(i = 0; i &lt; len; i++)
      {
          ch = ptr[i];
  
          switch(ch) 
          {
               case ';':
                   gcomc++;
                   break;
  
               default:
                   if (gcomc == 3) {
                       ch = ptr[i];
                       so[gpos++] = ch;
                   }
                   if (gcomc == 7) {
                       so[gpos] = '\0'; /* ensure so is null terminated */
                       printf(" %s ", so);
                       gcomc = 0;
                       gpos = 0;
                   }
          }
      }
  
  return;
  }
  
  extern int main() 
  {
  FILE *fin=fopen("test.txt", "rb");
      char buffer[10 + 1];
      size_t bsz;
  
      while((bsz = fread(buffer, sizeof *buffer, sizeof buffer, fin)) &gt; 0)
  {
      test(buffer, bsz);
  }
  
  return 1;
  }
  </code></pre>