Right now I am learning how to use C++ programming on Linux.
I just learned how to copy a file, now the question is, how can you tell if a file has been copied or not? I think I need a command or a function that allows me to check whether if the copied file does exist in the destination folder. Can anyone please give me a suggestion. I think it doesn't really matter which platform I am using. The fact that it's still C++ language doesn't change.

Thanks a lot!!

Dani AI

Generated

Several people here gave useful starting points (thanks to , , and ). There are actually levels of certainty you can aim for — existence, metadata match, byte-for-byte equality, and cryptographic verification — and the right choice depends on how much risk you can tolerate.

A practical, modern C++ approach (C++17+) is to make your copy routine report success, then do a quick verification with std::filesystem: check that the destination exists, is a regular file, and that file sizes match. That catches obvious failures very cheaply:

#include <filesystem>
namespace fs = std::filesystem;

bool quick_verify(const fs::path& src, const fs::path& dst) {
    try {
        if (!fs::exists(dst) || !fs::is_regular_file(dst)) return false;
        return fs::file_size(src) == fs::file_size(dst);
    } catch (...) { return false; }
}

For stronger guarantees combine that with a content check. For small files a byte-by-byte comparison is fine (as suggested by and demonstrated by ). For large files, prefer streaming verification or a cryptographic hash (SHA‑256) computed in chunks for both files and then compared — this avoids loading entire files into memory and gives high confidence that contents match. On POSIX, to avoid partial-file races write to a temporary filename, call fsync on the FD (or FlushFileBuffers on Windows), then atomically rename the temp file to the final name.

Checklist summary:

  • Have the copy function return/throw on error ().
  • Quick check: existence + file_size (std::filesystem).
  • Strong check: streaming byte-compare or compute/compare SHA‑256.
  • Production safety: write to a temp file, ensure flush to disk, then atomic rename.

These steps combine the simple checks already suggested in the thread with robust, production-safe practices — pick the level of verification that matches your reliability needs.

Recommended Answers

All 10 Replies

Member Avatar for Member #327959

Hi,

you can try opening the copied file (etihere in read only or write), if its opened successfully it means that its available.. right?

Hi,

you can try opening the copied file (etihere in read only or write), if its opened successfully it means that its available.. right?

Well if it's that simple I wouldn't have posted a thread here. I need a C++ command to the check whether if the file exists but not manual checking.

Member Avatar for Member #327959

Well.. I am also interested in knowing that. But As far my knowledge there is no such interface available.

Gives you the liste of functions fstream has. I just did a quick scan.. and it doesnt have any function related to our query..

I just learned how to copy a file, now the question is, how can you tell if a file has been copied or not? I think I need a command or a function that allows me to check whether if the copied file does exist in the destination folder. Can anyone please give me a suggestion.

Write yourself a function to copy a file, so that it e.g. returns false if copying fails, else true. I.e. if your function returns true, then you know that the file was copied (and hence no need for any extra checking).

you can also call standard C function fstat() or stat(), which will return error if the file is not found. And if it is found it will tell you how big it is -- you can use that info to compare with the original file to see if they are the same size.

If you can't write the code to copy a file correctly, what makes you think you'll fair any better at writing the code to check that you copied the file correctly?

AD's simplistic approach would only confirm that the number of writes matched the number of reads, but you could still have filled the file with zeros or garbage.

AD's simplistic approach would only confirm that the number of writes matched the number of reads, but you could still have filled the file with zeros or garbage.

Agree -- the only true way to determine if the file was written correctly is to compare the two files. There are diff programs available that will do that -- compare two files and display their differences, if any.

STL fun:

#include <algorithm>
#include <fstream>
#include <iterator>
#include <string>

bool file_compare(
  const std::string& filenameA,
  const std::string& filenameB,
  bool is_binary
  ) {
  std::ios::openmode mode = is_binary ? std::ios::binary : std::ios::in;
  std::ifstream fileA( filenameA.c_str(), mode );
  std::ifstream fileB( filenameB.c_str(), mode );
  if (!fileA or !fileB) return false;

  if (is_binary)
    {
    fileA >> std::noskipws;
    fileB >> std::noskipws;
    }

  return std::equal(
    std::istream_iterator <char> ( fileA ),
    std::istream_iterator <char> (),
    std::istream_iterator <char> ( fileB )
    );
  }

(Any open files are automatically closed when the ifstream goes out of scope.)

For binary comparison, every byte of the files must match.
For non-binary, whitespace is ignored (including at EOF).

Have fun!

1) Copy the file
2) close the files
3) Open both files for read
4) read each file byte by byte and make sure both bytes are identical. If not, bad copy.
5) if you hit EOF at the same time, the files are identical
6) close the files
7) output the status if necessary

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.