in one of the question for beginners a program for moving a file was asked. i cud not find the algo can anyone suggest me the algo????????????

Dani AI

Generated

— a practical, portable pattern for a “move file” routine in C++ is: try an atomic rename first (fast, preserves metadata and is usually instantaneous on the same filesystem). If that fails (different device/mount, permissions, or locks), fall back to a streamed copy in fixed-size chunks, verify the copy, then remove the original. That covers speed, correctness, and large-file safety. was right to point out OS APIs can be used; was right about buffering — the example below combines both approaches and adds simple verification.

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

bool move_file(const std::filesystem::path& src, const std::filesystem::path& dst) {
    std::error_code ec;
    std::filesystem::rename(src, dst, ec);      // fast/atomic when possible
    if (!ec) return true;

    // fallback: streamed copy (chunked)
    std::ifstream in(src, std::ios::binary);
    std::ofstream out(dst, std::ios::binary | std::ios::trunc);
    if (!in || !out) return false;

    std::vector<char> buf(64 * 1024);
    while (in) {
        in.read(buf.data(), buf.size());
        std::streamsize n = in.gcount();
        if (n > 0) out.write(buf.data(), n);
    }
    out.flush();
    if (!out) { std::filesystem::remove(dst, ec); return false; }

    // simple verification: compare sizes, then remove source
    std::error_code e1, e2;
    auto ssz = std::filesystem::file_size(src, e1);
    auto dsz = std::filesystem::file_size(dst, e2);
    if (e1 || e2 || ssz != dsz) { std::filesystem::remove(dst, ec); return false; }

    std::filesystem::remove(src, ec);
    return !ec;
}

Notes and cautions: copy-to-temporary-then-rename at the destination reduces chance of leaving a partial file visible; for stronger integrity checks use a checksum before deleting the source; preserve timestamps/permissions explicitly if needed; watch for locks and permissions. If targeting older compilers, use platform APIs (MoveFileEx/CopyFileEx on Windows, POSIX rename/read/write on Unix) with the same rename-then-fallback logic.

Recommended Answers

All 3 Replies

If you are using MS-Windows then you could call win32 api function CopyFile() to copy the file then delete the original. Otherwise, just write your own copy function to copy the file.

If you need to create your own copy function, first open the original file for reading, then create (or a file with that name exists) for writing. Then you could simply read a byte in the original file, then write that byte to the new file. This is simple to program, however very inefficient in terms of time if it is being copied to the same disk. A better solution might be to create a buffer (just an array of bytes), fill the buffer then write the buffer. Hope this helps.

>>A better solution might be to create a buffer (just an array of bytes), fill the buffer then write the buffer

But that won't work for huge files.

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.