title: File I/O in C language
body: <p>I am facing a problem with the function below. I am trying to get data from one location and then search for a specified string. After that I am printing the resulting value.
  For the first time it's working fine. If I call the function using a for loop however I am unable to print the buffer value.</p>
  
  <pre><code>void parse_data(char *fname,int flag)
  {
      char str[30]="&amp;lt;Response&amp;gt;";
      char buffer[1024],temp[1024],temp1[1024];
      int nVal=0;
      FILE *fp;
      int s_pos; //string position in the text
      int c_pos; //char position in the text
      char *string;
      char ccnt; //char count
      long lSize;
      long pos=0;
      int c;
      s_pos = -1;
      c_pos = 0;
      fp=fopen(fname,"r");
      //fseek(fp, 1, SEEK_SET);
      string = malloc(strlen(str)+1);
      if(fp==NULL)
      {
          printf("Unable to open the file \n");
          exit(0);
      }
  
      while (!feof(fp))
      {
          if (c_pos == 0)
          {
              for (ccnt = 1; ccnt &lt;= strlen(str); ccnt++)
              {
                  if (!feof(fp))
                  {
                      string[ccnt - 1] = getc(fp);
                      if(nVal==1)
                      {
                          buffer[pos++] =   string[ccnt -1];
                      }
                  }   //if
              }//for
  
          }//if
          if (c_pos != 0)
              if (!feof(fp))
              {
                  for (ccnt = 0; ccnt &lt;= strlen(str) - 2; ccnt++)
                      string[ccnt] = string[ccnt + 1];
                  string[strlen(str) - 1] = getc(fp);
                  if(nVal==1){
                      buffer[pos++] =   string[strlen(str) - 1];
                  }
              }
          if (strcmp(string, str) == 0)
          {
  
              strcpy(str,"&amp;lt;/Response&amp;gt;");
  
              s_pos = c_pos;
              if(nVal==1){
                  buffer[pos-strlen(str)]='\0';
                  break;
              }
              nVal=1;
  
          }
          c_pos++;
      }
      if(fp!=NULL)
          fclose(fp);
      //printf("\n The String position is %d=\n",s_pos);
      if(flag==0)
          ParsingString_Inserting_To_DataBase(buffer);
      else if(flag==1)
          printf("The Buffer Value is %s \n",buffer);
  }
  
  int main()
  {
      int i=0;
      char fname[30]="/tmp/gcc_trans.html";
      for(i=0;i&lt;3;i++)
          parse_data(fname,1);
      return 0;
  }
  </code></pre>