I'm trying to use sequential binary file editing in C++. I use a temporary file. I have to make the changes in the file by storing the modified data in the temp file. Then I delete the original file and rename the temp as the original. The problem is the remove() and rename() functions in <fstream.h> don't seem to be working. When I check in the folder after execution, the temp file is still there and so is the original file. Does any1 know an alternative method to remove and rename a file during runtime?

Dani AI

Generated

A common cause of the behavior in this thread is an open file handle preventing the OS from removing or renaming the file. resolved the immediate issue by closing the streams, and ’s nudge to check return values is important — good error reporting shows whether the OS refused the operation and why.

On Windows an open handle usually blocks delete/rename; on POSIX the directory entry can be removed while the file stays alive until the last descriptor is closed. That cross-platform difference matters when relying on atomic replace behavior. For robust code prefer C++17's filesystem utilities with explicit error handling, or fall back to the C functions while checking errno.

Example pattern (C++17):

#include <fstream>
#include <filesystem>
#include <system_error>

std::error_code ec;
{
    std::ofstream tmp("data.tmp", std::ios::binary);
    // write and let destructor close the stream when leaving scope
}
std::filesystem::rename("data.tmp", "data.bin", ec);
if (ec) {
    // handle: maybe remove existing target then retry, report ec.message()
}

Fallback (pre-C++17) with explicit checks:

if (std::remove("data.bin") != 0) std::perror("remove");
if (std::rename("data.tmp","data.bin") != 0) std::perror("rename");

Troubleshooting tips: ensure every fstream/filebuf is closed (call .close() or limit scope), check return values or catch filesystem exceptions, and inspect open handles (Process Explorer on Windows) if a delete keeps failing. See the C API docs (rename/remove) and the C++ filesystem reference (std::filesystem::rename) for platform semantics and error details.

Recommended Answers

All 2 Replies

I'm trying to use sequential binary file editing in C++. I use a temporary file. I have to make the changes in the file by storing the modified data in the temp file. Then I delete the original file and rename the temp as the original. The problem is the remove() and rename() functions in <fstream.h> don't seem to be working. When I check in the folder after execution, the temp file is still there and so is the original file. Does any1 know an alternative method to remove and rename a file during runtime?

Do you have a code sample that demonstrates the problem? Are you checking return values?

I didn't know I had to close all variables connected to the file to be deleted. It worked after I did that. Thanks a ton Dave. :)

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.