C语言中remove/rename函数对accountInformation.txt失效,其他文件正常
remove() and rename() Failures on accountInformation.txt Hey Justin, let's break down why your remove() and rename() calls are failing specifically on accountInformation.txt—even though they work on other files. Since you're already digging into errno.h, we can use that to narrow things down quickly.
Most Likely Culprits (and Fixes)
You forgot to close the file handle
This is the #1 reason for this kind of issue. If your code opensaccountInformation.txtto copy data, but doesn't callfclose()on it before trying to delete/rename, the operating system will block the operation because the file is still in use by your process.Double-check your copy logic:
FILE *acc_file = fopen("accountInformation.txt", "r"); // ... copy data to temp.txt ... fclose(acc_file); // Make sure this line exists!If you skip
fclose(),errnowill likely throwEBUSYorEACCESwhen you callremove().Permission or ownership issues
Even if other files work,accountInformation.txtmight have restrictive permissions (like read-only) or be owned by a different user than the one running your program. Check the file's permissions (on Unix usels -l, on Windows right-click > Properties > Security) and ensure your process has write/delete access to it.If
errnoreturnsEACCESorEPERM, this is the problem. You can temporarily adjust permissions to test, or run your program with elevated privileges if needed.Incorrect file path (hidden working directory mismatch)
Sometimes your program's current working directory isn't what you expect, soremove("accountInformation.txt")is looking for the file in the wrong folder. Try using the full absolute path to the file instead, like:remove("/home/you/your-project/accountInformation.txt"); // Unix remove("C:\\Users\\you\\your-project\\accountInformation.txt"); // WindowsIf this works, your relative path was the issue. You can use
getcwd()(Unix) orGetCurrentDirectory()(Windows) to confirm your program's working directory.File locked by another process
Antivirus tools, backup software, or even another instance of your program might haveaccountInformation.txtopen. This will blockremove()andrename()from modifying the file.Check
errnoforEBUSY—if you see that, close any other programs that might be accessing the file, or restart your system to release the lock.
Use errno to Get Precise Details
Since you're already using errno.h, add explicit error messages to pinpoint the issue. Here's how to expand your error checking:
#include <stdio.h> #include <errno.h> #include <string.h> // ... your copy logic here ... // Check remove() failure if (remove("accountInformation.txt") != 0) { fprintf(stderr, "Failed to delete accountInformation.txt: %s\n", strerror(errno)); switch(errno) { case EBUSY: fprintf(stderr, "Tip: File is locked by this process or another program.\n"); break; case EACCES: fprintf(stderr, "Tip: Permission denied—check file permissions.\n"); break; case ENOENT: fprintf(stderr, "Tip: File not found—verify the path is correct.\n"); break; } } // Check rename() failure if (rename("temp.txt", "accountInformation.txt") != 0) { fprintf(stderr, "Failed to rename temp.txt: %s\n", strerror(errno)); }
Start with checking for unclosed file handles—it's the most common fix. If that doesn't work, use the errno output to follow the trail to permissions, path, or locking issues.
内容的提问来源于stack exchange,提问作者Justin Hawry

