[英]boost filesystem copy_file “successful” but no files copied
我在弄清楚為什么我的文件無法復制時遇到麻煩。 這是代碼的簡短部分:
( dir_itr
是directory_iterator,而root
是路徑)
if (!(is_directory(dir_itr->path())))
{
cout << "copying: " << dir_itr->path().filename() << endl;
try
{
copy(dir_itr->path(), root);
remove(dir_itr->path());
} catch (filesystem_error& ex) {
//more code
結果在命令窗口中如下所示:
boost::filesystem::copy_file: The operation completed successfully:
"C:\Documents and Settings\R\Desktop\New Folder\New Folder (2)\New Bitmap Image 3.bmp",
"C:\Documents and Settings\R\Desktop\New Folder"
但是,沒有文件被復制。
我基本上只是想將所述文件從文件夾c:\\x\\y\\file.file
到c:\\x
我假設為什么我不能移動它是因為我需要一個完整的文件名,而不僅僅是目錄或其他內容? 在這種情況下,如何將路徑根轉換為字符串,以便可以向其中添加文件名? (如果我什至嘗試,我會收到一千個錯誤,它們是如此之久,以至於我無法一直滾動到整個窗口以查看其開始位置)
也許boost :: filesystem :: system_complete可以幫助:
(對不起,我在Mac上而不是Windows上,但是它顯示了一種從相對路徑獲取絕對路徑的方法)。 祝好運。
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
int main(int argc, char *argv[]) {
boost::filesystem::path cwd(".");
boost::filesystem::path resolved = boost::filesystem::system_complete(cwd);
std::cout << cwd << std::endl;
std::cout << resolved << std::endl;
}
輸出:
"."
"/private/var/folders/qw/x23nm9f11fxc45rgddb04n_w0000gn/T/CodeRunner/."
回到工作上,我添加/更改了以下內容:
try
{
string temp = root.string() + "\\" + dir_itr->path().filename().string();
path p(temp);
copy(dir_itr->path(), p);
remove(dir_itr->path());
//more code
它似乎有效。 我猜想我的假設是在復制正確時需要包括文件名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.