Close the file with fclose (), but the file is still in use

I had a problem deleting / rewriting a file using my program, which is also used (read) by my program. The problem is that, because my program reads data from a file (output.txt), it puts the file in an "in use" state, which makes it impossible to delete or overwrite the file.

I don’t understand why the file remains β€œin use” because I close the file after use with fclose ();

this is my 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 } } 

I use fclose () to close the file, but the file remains in use by my program until my program is completely disabled.

Which solution frees the file so that it can be deleted / overwritten?

In fact, my code is not a loop without end; )

Thanks in advance!

Marco

Update

I like to request the part of my code that also generates the file 'in use'. This is not a loop, and this function is called from main ();

Here is the code snippet:

 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<9;i++){ fscanf(pInputFile, "%d", &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); } 

The while loop parses the file. The output.txt file contains sets of 9 variables, the number of sets is unknown, but always in sets of 9.

output.txt may contain, for example: 0,1,2,3,4,5,6,7,8,8,7,6,5,4,1,2,3,0

update 2

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 } } 

sorry for using pre again, but I am not getting the formatting of this site :(

+4
source share
5 answers

Will this be due to maximum disk space, and there is still data in the file stream buffer; fclose'ing the file stream flushes it (writes all data to the buffer), the write operation will end with an error, since the maximum disk space will be reached.

I suggest you fix the problem by calling fclose () immediately after fopen (). If this is a success, then something is wrong in the code between fclose () and fopen ().

+1
source

Perhaps there are other places in your code where you do not call fclose , leaking the file. Even in this code, if an error occurs between fopen and fclose (either with the return statement or the continue statement, etc.), you will skip the file. Please switch to the RAII idiom.

Edit: include this in your code:

 struct PoorMansFile { FILE *_file; PoorMansFile(const char* str1, const char* str2) : _file(fopen(str1,str2)) {} ~PoorMansFile() { if(_file) fclose(_file); } operator FILE*() const { return _file; } }; int fclose(PoorMansFile& file) { if(!file) return 0; int t = fclose(file._file); file._file = 0; return t; } 

and replace each

 FILE* file = NULL; file = fopen(str1, str2); 

with:

 PoorMansFile file(str1, str2); 

Let us know if this helps;

0
source

A file can still be used by a CRT or an OS β€” for example, an OS can buffer writes to disk. fflush () will only flush CRT buffers, not OS buffers.

0
source

Just in the dark here ...

What is inside runProgram() ? Does this function wait until the program terminates before returning? I wonder if the program that writes the data really still works ... it’s hard to say from here, but I thought that I would throw it away!

0
source

After reading all the answers and comments, I could not think of any reason for the OP problem.

Is this a multithreaded or re-program?

What happens if fopen twice and fclose twice in the same file? Could this be causing the problem?

In this thought, I propose two more checks.

  • printf all fopen / fclose call
  • after fclose reset the file variable to NULL

f = fopen ("," "); printf (" fopen =>% p ", f);
fclose (e); printf ("fclose =>% p", f); f is 0;

If you are inconvenient when debugging printf, you can use OutputDebugString.

extern void __stdcall OutputDebugStringA(const char*) (MS VC only)

-1
source

Source: https://habr.com/ru/post/1335213/


All Articles