copying a file in C++17
-
I wrote some code to copy a file:
#include
#includenamespace fs = std::filesystem;
int main() {
fs::path source_file("source.txt");
fs::path destination_file("destination.txt");try { fs::copy\_file(source\_file, destination\_file); std::cout << "File copied successfully!" << std::endl; } catch (const fs::filesystem\_error& e) { std::cerr << "Error copying file: " << e.what() << std::endl; } return 0;
}
Is this a good way of doing it? Anyone know what buffer sizes are being used behind the scenes? Thanks.
-
I wrote some code to copy a file:
#include
#includenamespace fs = std::filesystem;
int main() {
fs::path source_file("source.txt");
fs::path destination_file("destination.txt");try { fs::copy\_file(source\_file, destination\_file); std::cout << "File copied successfully!" << std::endl; } catch (const fs::filesystem\_error& e) { std::cerr << "Error copying file: " << e.what() << std::endl; } return 0;
}
Is this a good way of doing it? Anyone know what buffer sizes are being used behind the scenes? Thanks.
mike7411 wrote:
Anyone know what buffer sizes are being used behind the scenes?
It is not recorded, but chances are it is calculated based on the size of the source file and the disk sector size. If you want to find out how particular STL features work then you can find lots of useful information at C++ reference - cppreference.com[^] or C++ Standard Library Reference | Microsoft Learn[^].
-
I wrote some code to copy a file:
#include
#includenamespace fs = std::filesystem;
int main() {
fs::path source_file("source.txt");
fs::path destination_file("destination.txt");try { fs::copy\_file(source\_file, destination\_file); std::cout << "File copied successfully!" << std::endl; } catch (const fs::filesystem\_error& e) { std::cerr << "Error copying file: " << e.what() << std::endl; } return 0;
}
Is this a good way of doing it? Anyone know what buffer sizes are being used behind the scenes? Thanks.
mike7411 wrote:
Is this a good way of doing it?
1. You want to check what happens if different drives are involved. 2. You want to verify paths are supported. 3. Catching one type of exception ignores possible other ones. Probably unlikely but in case.
mike7411 wrote:
buffer sizes are being used behind the scenes?
There are all sorts of possible buffers. Disk, OS, library. Only concern however for that is speed. You can profile it. You can also use a OS command shell call for comparison. If it matters, at least in my experience, OS shell commands will always be faster. This is especially true when copying directories. Seems reasonable given that the copy operation in the OS doesn't involve loading the data into the application. Even so if you need it to be 'fast' for some reason then I would suggest that you need to change your requirements/design. Copying files, in general, is always 'slow'. Speed doesn't matter for single small files. So only matters for very large files and/or large numbers of files. But those will always be 'slow'. And there can be error conditions that make it even slower (which your code does not account for.) So attempting to guarantee a speed rate is never going to work.