title: C remove() function not working
body: <p>I'm trying to implement a program I wrote on my Mac on Windows systems, and I'm running into a lot of trouble with this last function:</p>
  
  <pre><code>/* clearFiles */
  //  deletes the section files after the program has executed
  //  skips any sections listed table that don't appear in the body (b/c no file was written for these)
  //  also deletes the table &amp; body files
  void clearFiles(section_t *tableArray, int *skipArray, char *tableName, char *bodyName)
  {
      int i, status, index;
      char str[SECTNAME];
      char command[SECTNAME];
  
      index = 0;
  
      //  clear section files
      for(i=1; tableArray[i].count!=0;i++)
      {
          if(i!=skipArray[index])
          {
  
              strcpy(str, tableArray[i].shortName);
              strcat(str,".txt");
  
              status = remove(str);
              if(status!=0)
                  printf("Warning! File %s not deleted.\n", str);
  
          } else index++;
      }
  
      //  clear table file
      status = remove(tableName);
      if(status!=0)
          printf("Warning! File %s not deleted.\n", tableName);
  
      //  clear body file
      status = remove(bodyName);
      if(status!=0)
          printf("Warning! File %s not deleted.\n", bodyName);
  }
  </code></pre>
  
  <p>The program takes a very large file and first splits it into a table of contents file and a body file. It then takes the body and splits it into hundreds of individual section files, from which it performs the actual task of the program. At the end, I want it to delete all of these extra files, because they just clutter up the directory. It works perfectly on my Unix Mac environment, but when I try to run it in Command Prompt on a PC, my remove() function returns -1 for every section file and doesn't delete it (it does, however, successfully delete the table &amp; body files). I also tried a more brute force method by using     system(del fileName), but this didn't work either, because it says that the file is being used by another process. I can't figure out why these files might be open, as every time fopen() appears, I follow it up with an fclose(). The exception is when checking if the files are open, I use </p>
  
  <pre><code>if(fopen(fileName,"r")!=NULL){}
  </code></pre>
  
  <p>Could this be the problem? Is there either a way to check if a file is open without actually opening it, or is there a way to close a file that was checked this way? I tried assigning a dummy pointer to it and coding:</p>
  
  <pre><code>dummy = fopen(fileName, "r");
  if(dummy!=NULL){}
  fclose(dummy);
  </code></pre>
  
  <p>But this didn't work either. Is it possible to pass just a file path to the fclose() function (for example, something similar to fclose(C:\users\USER\desktop\fileName.txt)? Also, I know that the program is attempting to delete the correct fileName, because my error message prints the correct names to the command prompt.</p>
  
  <p>Any input is greatly appreciated!!! Thanks.</p>
  
  <p>NOTE: 
     The tableArray starts at 1 because of a search function implemented in the program that returns an index if found and 0 if not found. In hindsight, it would have been better to return -1 if not found and start the index at zero, but that is a separate issue</p>
  
  <p>UPDATE:</p>
  
  <p>Below is the code used to create the section files:</p>
  
  <pre><code>if(fopen(word, "r")==NULL){
      ofile = fopen(word, "w");
  
      fprintf(ofile, "SECTION %s ", section);
  
      //go until end of file or until found the next section
      //  bug fix: check the section after that, too (in case the next section isn't there)
      while(fscanf(spec, "%s", word)!=EOF &amp;&amp; !cease)
      {
          if(strcmp(word,"SECTION")!=0){
              fprintf(ofile, "%s ", word);
          }
          else{
              fscanf(spec, "%s", word);
              choice = testNumber(spec,word);
              for(j=i+1; tableArray[j].count!=0;j++)
                  if(strcmp(word,tableArray[j].shortName)==0)
                      cease = 1;
          }
      }
  
      fclose(ofile);
  }
  </code></pre>