title: close file with fclose() but file still in use
body: <p>I've got a problem with deleting/overwriting a file using my program which is also being used(read) by my program. The problem seems to be that because of the fact my program is reading data from the file (output.txt) it puts the file in a 'in use' state which makes it impossible to delete or overwrite the file.</p>
  
  <p>I don't understand why the file stays 'in use' because I close the file after use with fclose(); </p>
  
  <p>this is my code:</p>
  
  <pre><code>bool bBool = true
  
  while(bBool){
    //Run myprogram.exe tot generate (a new) output.txt
  
    //Create file pointer and open file
    FILE* pInputFile = NULL;
    pInputFile = fopen("output.txt", "r");
    //
    //then I do some reading using fscanf()
    //
    //And when I'm done reading I close the file using fclose()
    fclose(pInputFile);
  
    //The next step is deleting the output.txt
    if( remove( "output.txt" ) == -1 ){
      //ERROR
    }else{
      //Succesfull
    }
  }
  </code></pre>
  
  <p>I use fclose() to close the file but the file remains in use by my program until my program is totally shut down.</p>
  
  <p>What is the solution to free the file so it can be deleted/overwrited?</p>
  
  <p>In reality my code isn't a loop without an end ; )</p>
  
  <p>Thanks in advance!</p>
  
  <p>Marco</p>
  
  <p><strong>Update</strong></p>
  
  <p>Like ask a part of my code which also generates the file 'in use'. This is not a loop and this function is being called from the main();</p>
  
  <p>Here is a piece of code:</p>
  
  <pre><code>int iShapeNr = 0;
  
  void firstRun()
  {
      //Run program that generates output.txt
      runProgram();
  
      //Open Shape data file
      FILE* pInputFile = NULL;
      int iNumber = 0;
      pInputFile = fopen("output.txt", "r");
  
      //Put all orientations of al detected shapes in an array
      int iShapeNr = 0;
      int iRotationBuffer[1024];//1024 is maximum detectable shapes, can be changed in RoboRealm
      int iXMinBuffer[1024];
      int iXMaxBuffer[1024];
      int iYMinBuffer[1024];
      int iYMaxBuffer[1024];
  
      while(feof(pInputFile) == 0){       
          for(int i=0;i&lt;9;i++){
              fscanf(pInputFile, "%d", &amp;iNumber);
              fscanf(pInputFile, ",");
              if(i == 1) {
                  iRotationBuffer[iShapeNr] = iNumber;
              }
              if(i == 3){//xmin
                  iXMinBuffer[iShapeNr] = iNumber;
              }
              if(i == 4){//xmax
                  iXMaxBuffer[iShapeNr] = iNumber;
              }
              if(i == 5){//ymin
                  iYMinBuffer[iShapeNr] = iNumber;
              }
              if(i == 6){//ymax
                  iYMaxBuffer[iShapeNr] = iNumber;
              }
          }
          iShapeNr++;
      }
      fflush(pInputFile);
      fclose(pInputFile);
  
  }
  </code></pre>
  
  <p>The while loop parses the file. The output.txt contains sets of 9 variables, the number of sets is unknown but always in sets of 9.</p>
  
  <p>output.txt could contain for example: 0,1,2,3,4,5,6,7,8,8,7,6,5,4,1,2,3,0</p>
  
  <p><strong>update 2</strong></p>
  
  <p>code:</p>
  
  <pre><code>    void runProgram(){
      //Check if output.txt exists, if so delete it
      if(fileExists("output.txt") == 1){
          //Delete output.txt
          if( remove( "output2.txt" ) == -1 ){
              //errormessage
          }else{
              //succesfull
          }
      }   
      //start program
      ShellExecute( NULL, TEXT("open"), TEXT("program.exe"), NULL, NULL, SW_SHOWMAXIMIZED);
  
      while(fileExists("output.txt") == 0);
  
      //Close program
      int iCheck = system("taskkill /IM program.exe");
      if(iCheck != 0){
          //error could not shut down
      }
  }
  </code></pre>
  
  <p>sorry for using pre again but I don't get the formatting of this site :(</p>