I wrote a program, in which my code is checking the word entered by user and the word in a file. If the words are not same, the file is deleted.
i closed the file using fclose(fp), (where fp is file pointer)
and to delete the file i used system("del filename.txt").
But, i get a messsage saying that the file is in use by another process.
My question is HOW?? When I closed the file then how it can be in use of some other process??
Need assistance on this ... thanks!!

Dani AI

Generated

A short explanation and practical next steps based on the thread and the final outcome reported by .

On Windows a "file in use by another process" means some process still holds an OS file handle to that path. That handle can come from the same program (another FILE* or a memory-mapped view), from a child process that inherited handles, or from some external program (Explorer, antivirus, indexer). Windows will prevent deletion unless the handle was opened with delete-sharing allowed; see the CreateFile documentation for how share flags affect this: CreateFile (Windows API).

How to find the holder (fast, reliable)

  • Use Sysinternals Process Explorer (run elevated) then Find->Find Handle or DLL and search the filename. The tool shows which process and handle is keeping the file. Process Explorer download/docs: Process Explorer.
  • Alternatively use the command-line Handle utility: Handle. Both show PID and handle value so you can close or investigate the process safely.

Practical fixes

  • Ensure every open is actually closed and no other part of your program keeps a handle (including memory-maps or library code). Closing earlier, as discovered, is often the real fix.
  • Prevent child processes inheriting handles (clear HANDLE_FLAG_INHERIT or use non-inheritable open flags).
  • If you need to allow deletion while a handle is open, open the file with FILE_SHARE_DELETE (Windows API), for example:
HANDLE h = CreateFileA("filename.txt",
                       GENERIC_READ|GENERIC_WRITE,
                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
                       NULL, OPEN_EXISTING, 0, NULL);
if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
DeleteFileA("filename.txt");

Caution: forcibly closing handles shown by Process Explorer can destabilize processes. Use the tools to identify the cause; then fix the code or configuration (sharing flags, inheritance, third-party software) rather than rely on sleeps or retries.

Recommended Answers

All 5 Replies

Try remove().

My question is HOW?? When I closed the file then how it can be in use of some other process??

My gut reaction is to say that this is a timing issue, but it's possible that fclose() failed. First try testing the return value to see if there were any problems:

if (fclose(fp)) {
    perror("Failed to close the file");
}

If fclose() succeeds then to check if it's a timing issue, where the OS is still holding onto the handle because flush and close requests are queued and haven't completed, you can try sleeping for a bit to give the OS time to catch up:

#include <stdbool.h>
#include <windows.h>

...

fclose(fp);

bool removed = false;

/* Try a few times with a pause inbetween */
for (int i = 0; i < 3; ++i) {
    if (remove("filename.txt") == 0) {
        removed = true;
        break;
    }

    Sleep(200);
}

if (!removed) {
    fputs("File was not deleted\n", stderr);
}

You may need to play with the pause amounts and number of attempts, but the above has worked well for me in catching any timing issues. 200ms is actually quite huge, and often far less than that works just fine.

Didn't work for me!! :(
it always displays stderr part. tried pausing the process for 1000 and 2000 too.. same thing happens... shall I post the actual code?

shall I post the actual code?

Please do, if it's not excessively long.

Please do, if it's not excessively long.

I solved the problem, by closing the file way before getting to this part of code.. it works fine now... But, I don't know why the Sleep() is not making any difference!! :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.