title: In C, how should I cut a text file between a char and another?
body: <p>I try to code a program cutting a text file between a character and another. To illustrate, <code>example.txt</code> that I want to cut includes numbers and some characters such as <code>'[[['</code> and <code>]]]</code> (they may chance as type and count of charters). These characters mark the start or end of the cutting process.</p>
  
  <p>This process looks like it is simple; however it may be tiring and carrying risk which is high possibility of making a mistake. Anyway while I am writing the question, I solve a lot of problems about my problem except saving to another text file. <code>[[[</code>'s count is uncertain: that is to say, I don't know how many files I have to create. I am open to new thoughts and ideas, no matter which programming language it is. </p>
  
  <h1><strong>Input file example and its outputs:</strong></h1>
  
  <p><strong>example.txt</strong></p>
  
  <blockquote>
    <p>005076,-0.00008389,0.00039446,-0.00007079,0.00024138,0.00059449,-0.000
    24860,<strong>[[[</strong>
    0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001  395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014
    975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458
    9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373
    0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945
    <strong>]]]</strong>
     0.00086406,0.00071132,-0.00081439,0.10930956,-0.06242029,-0.21353303,0  00003668,-0.00000001,0.00088168,-0.03589109,-0.00000584,0.00366844,0.0
    <strong>[[[</strong> 0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,-
     0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0   090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012
    73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176 <strong>]]]</strong></p>
  </blockquote>
  
  <p><strong>cut1.txt</strong></p>
  
  <blockquote>
    <p>0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001  395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014
    975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458
    9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373
    0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945</p>
  </blockquote>
  
  <p><strong>cut2.txt</strong></p>
  
  <blockquote>
    <p>0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,-
     0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0   090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012
    73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176</p>
  </blockquote>
  
  <h1><strong>Compiler system information:</strong></h1>
  
  <pre><code>$ cygcheck --version
  cygcheck (cygwin) 2.0.4
  System Checker for Cygwin
  Copyright (C) 1998 - 2015 Red Hat, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  
  $ gcc --version
  gcc (GCC) 4.9.2
  Copyright (C) 2014 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  </code></pre>
  
  <h1>Compile information</h1>
  
  <blockquote>
    <p>$ gcc -o cutt cutt.c </p>
    
    <p>$ ./cutt</p>
    
    <p>0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001 395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014
    975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458
    9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373
    0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945
    <strong>]][</strong> 0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,- 0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0 090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012
    73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176</p>
  </blockquote>
  
  <p>If so, how many <code>[[[</code> and <code>]]]</code> v <code>cutN.txt</code> files. needed.</p>
  
  <h1><strong>Cutt's code:</strong></h1>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  #define MAX_LEN 1024
  
  int testp=0, r=0;
  char cx[MAX_LEN],cz[MAX_LEN];
  void Cutt(char[],char[],int);
  
  main()
  {
      FILE *fpR, *fpW;
      char ch[MAX_LEN],sEArch4[3];
      int chc,i=0,cxc;
      sEArch4[1]=0x5b;sEArch4[2]=0x5b;sEArch4[3]=0x5b;
  
      fpR = fopen("example.txt","r");
      if (fpR==NULL) { 
          printf("Could not open example.txt !\n"); 
          return 1;
      }
  
      fpW = fopen("cut1.txt","w");
      if (fpW==NULL) { 
          printf("Could not open cut1.txt!\n"); 
          return 1;
      }
  
      while(fgets(ch,MAX_LEN,fpR)) {
          //printf("%s",ch);
          chc = strlen(ch);
          for(i=0;i&lt;chc;i++)
              //printf("%s %d %d",ch,i,chc);
              Cutt(ch,sEArch4,i);
      }
      //printf("%s",cx);
      cxc = strlen(cx);
      for(i=1;i&lt;cxc-2;i++)
          cz[i-1] = cx[i];
      printf("%s",cz);
  //Jump; 
      return 0;
  }
  void Cutt(char ch[],char sEArch4[],int i)
  {
      int j,k=0;
      for (j=i ; j&lt;i+strlen(sEArch4) ; j++ ) {
          if(ch[j]!=sEArch4[k]){
              if ( ch [j] == 0x5b &amp;&amp; ch [j-1] == 0x5b &amp;&amp; ch [j-2] == 0x5b ) {
                  testp = 1;
              }
              if ( ch [j] == 0x5d &amp;&amp; ch [j-1] == 0x5d &amp;&amp; ch [j-2] == 0x5d ) {
                  testp = 2;
              }   
              if(testp==1) {  
                  cx[r] = ch[j];
                  r++;
                  return;
              }
              //printf("testp:%d \nch:%s\n",testp,x);
              if(testp==2)
                  return;
              //goto Jump;
              else
                  return; 
              k++;
          }
      }
  }
  </code></pre>
  
  <p><strong>Note:</strong>
  For single cut, it runs free of problems. for more than one cut, there is  <code>]][</code> chars. Look at compile information. I think it will use for cut.</p>