最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

I can't move files through C++ (both MoveFile, MoveFileEx, filesystem::rename) - Stack Overflow

matteradmin12PV0评论

I was trying to write a simple program to move files from directory A to directory B, in the end the file moving part keeping giving ACCESS DENIED errors and what so. Here is the code

const char* src = "C:\\Users\\user1\\testfolder\\source\\test.txt";
const char* dest = "C:\\Users\\user1\\testfolder\\destination";


try {
    std::filesystem::rename(src, dest);
} catch (std::filesystem::filesystem_error& e) {
    std::cout << e.what() << '\n';
}
return 0;

This prints the error ACCESS DENIED

VS2022 is running as administrator. C++ Version is C++ 17. The file can be moved through cmd with no problem.

MoveFile exits with error 183 MoveFileEx(src,dest,MOVEFILE_REPLACE_EXISTING) exits with error 5 (ACCESS DENIED)

I tried restarting VS, running as admin, and tried other file moving solutions, none seem to work.

I was trying to write a simple program to move files from directory A to directory B, in the end the file moving part keeping giving ACCESS DENIED errors and what so. Here is the code

const char* src = "C:\\Users\\user1\\testfolder\\source\\test.txt";
const char* dest = "C:\\Users\\user1\\testfolder\\destination";


try {
    std::filesystem::rename(src, dest);
} catch (std::filesystem::filesystem_error& e) {
    std::cout << e.what() << '\n';
}
return 0;

This prints the error ACCESS DENIED

VS2022 is running as administrator. C++ Version is C++ 17. The file can be moved through cmd with no problem.

MoveFile exits with error 183 MoveFileEx(src,dest,MOVEFILE_REPLACE_EXISTING) exits with error 5 (ACCESS DENIED)

I tried restarting VS, running as admin, and tried other file moving solutions, none seem to work.

Share Improve this question edited Nov 16, 2024 at 2:53 President James K. Polk 42.1k30 gold badges110 silver badges146 bronze badges asked Nov 15, 2024 at 21:41 クッキーくんクッキーくん 131 silver badge2 bronze badges 1
  • 3 Don't you need to include the destination file name in dest? – Michael Liu Commented Nov 15, 2024 at 21:44
Add a comment  | 

1 Answer 1

Reset to default 2

There is at least one problem with your dest path value, possibly two problems.

  1. If the source in a call to std::filesystem::rename is a non-directory file (as yours is), then the destination must also be a non-directory file.

  2. Even if the new file does not (yet) exist, the directory into which you are placing it must exist (third second-level bullet in the page linked below).

So, first make sure that your C:\\Users\\user1\\testfolder\\destination folder exists, then add the destination filename to the specified target path, as below:

const char* dest = "C:\\Users\\user1\\testfolder\\destination\\test.txt";

Further reference: cppreference

Post a comment

comment list (0)

  1. No comments so far