R: [Boost-users] filesystem::copy_file throws -- but why?
I think you should rewrite your code either as: int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.string() + ".bak"); fs::copy_file(fileName, copyFileName); } or as: int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.native_file_string() + ".bak", fs::native); fs::copy_file(fileName, copyFileName); } In your example you are creating a fs::path from a string in "native" form by using the default constructor, which expects the string to be in "generic" form. Hope this helps. Regards, Michele Galante Zucchetti Centro Sistemi s.p.a. m.galante@centrosistemi.it -----Messaggio originale----- Da: Scott Meyers [mailto:Usenet@aristeia.com] Inviato: giovedì 11 dicembre 2003 7.11 A: boost-users@lists.boost.org Oggetto: [Boost-users] filesystem::copy_file throws -- but why? [...] int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.native_file_string() + ".bak"); fs::copy_file(fileName, copyFileName); } This throws an exception: boost::filesystem::path: invalid name "D:\Temp\CDFix\INDEX.HTM.bak" in path: "D:\Temp\CDFix \INDEX.HTM.bak" [...]
On Thu, 11 Dec 2003 08:32:06 +0100, Galante Michele wrote:
int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.string() + ".bak"); fs::copy_file(fileName, copyFileName); }
I had tried this before. It also throws.
or as:
int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.native_file_string() + ".bak", fs::native); fs::copy_file(fileName, copyFileName); }
This works. Thanks very much! Scott
At 12:57 PM 12/11/2003, Scott Meyers wrote:
On Thu, 11 Dec 2003 08:32:06 +0100, Galante Michele wrote:
int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.string() + ".bak"); fs::copy_file(fileName, copyFileName); }
I had tried this before. It also throws.
It would have to be:
int processFile(const fs::path& fileName)
{
fs::path copyFileName(fileName.string() + ".bak", fs::native);
fs::copy_file(fileName, copyFileName);
}
I'd probably write it like this, just for stylistic reasons:
int processFile(const fs::path& fileName)
{
fs::copy_file(fileName,
fs::path(fileName.string() + ".bak", fs::native));
}
Also, if you haven't already done something similar, change the name of
your main() function to cpp_main, after adding:
#include
Beman Dawes wrote:
At 12:57 PM 12/11/2003, Scott Meyers wrote:
On Thu, 11 Dec 2003 08:32:06 +0100, Galante Michele wrote:
int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.string() + ".bak"); fs::copy_file(fileName, copyFileName); }
I had tried this before. It also throws.
It would have to be:
int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.string() + ".bak", fs::native); fs::copy_file(fileName, copyFileName); }
I don't see why. string() returns a path "formatted according to the rules of the generic path string grammar", i.e. '/' separator. Aren't native paths allowed to use a ':' separator with '/' being a legitimate part of the name, for instance? Why does the original fs::path copyFileName(fileName.string() + ".bak"); throw? I see nothing that could cause fileName.string() + ".bak" to fail validation. Am I missing something, or is this a bug?
At 09:03 AM 12/12/2003, Peter Dimov wrote:
Beman Dawes wrote:
It would have to be:
int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.string() + ".bak", fs::native); fs::copy_file(fileName, copyFileName); }
I don't see why. string() returns a path "formatted according to the rules of the generic path string grammar", i.e. '/' separator. Aren't native paths allowed to use a ':' separator with '/' being a legitimate part of the name, for instance?
Yep. My mistake. See below.
Why does the original
fs::path copyFileName(fileName.string() + ".bak");
throw? I see nothing that could cause fileName.string() + ".bak" to fail validation. Am I missing something, or is this a bug?
(What follows is based on current CVS version) It throws because the name_check was set to portable_name, and that doesn't allow ':' as a character. Because the function's fileName argument is already a path, it is known to have already been check by an appropriate name checker. Furthermore, if appending ".bak" to copyFileName invalidates it, that will be detected right away by the copy_file call. So not checking at all might be appropriate: void processFile(const fs::path& fileName) { fs::path copyFileName(fileName.string() + ".bak", fs::no_check); fs::copy_file(fileName, copyFileName); } But Michele Galante's original suggestion is perhaps better: int processFile(const fs::path& fileName) { fs::path copyFileName(fileName.native_file_string() + ".bak", fs::native); fs::copy_file(fileName, copyFileName); } --Beman
participants (4)
-
Beman Dawes
-
Galante Michele
-
Peter Dimov
-
Scott Meyers