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!!

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.

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

This article has been dead for over six months. Start a new discussion instead.