copyfile error 5 access denied!

cant copy file i get error 5 access denied i disabled uac and my account has admin rights and its not helping me. i checked the directories and they exist .
Last edited on
Can you open the file in notepad from the GUI?

Just because you're admin, doesn't mean you have automatic access to everything. If you're excluded using the ACL, you may need to take ownership or use your backup privilege.
what is acl?
i have console application , how i manage to use copyfile. are there other functions to copy file without the need of the user to manually take ownership and things like that?
Just in case this gets edited out, the OP's post was this:
Leonardo321 wrote:
cant copy file i get error 5 access denied i disabled uac and my account has admin rights and its not helping me. i checked the directories and they exist .


If the file is opened and is being used by another program or OS, there's no point in trying. Windows will refuse to give you access regardless.

Can you describe the method you're using to copy the file?
Last edited on
the file i want to copy is just used by my appilcation it copy my document from my usb to my pc but not working!
That really doesn't help us at all. Do you happen to have any code?

Try reading it's contents in a buffer, then creating a new file and putting it on the hard drive.

I've whipped up an example in about 10 minutes, so it may not be the best way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdint>

int main()
{
	std::string    filename[2]; // Requested file name
	std::ifstream   in;         // The file i/o stream
	std::ofstream   out;        // The file i/o stream
	std::streampos size;        // The file size
	
	std::vector<uint8_t> buffer; // Buffer to hold file contents
	
	// Retrieve the file
	std::cout << "Enter the name of the file you'd like to load: ";
	std::getline(std::cin, filename[0]);
	std::cout << "\n";
	
	std::cout << "Where do you want the copy to end up?: ";
	std::getline(std::cin, filename[1]);
	std::cout << "\n";
	
	// Attempt to open the file
	in.open(filename[0]);
	
	// If we can't open the file...
	if(!in) {std::cout << "Cannot open file.\n"; std::cin.get(); return 1;}
	
	std::cout << "Getting file parameters...\n";
	
	// Obtain file size
	in.seekg(0, std::ios::end);
	size = in.tellg();
	
	std::cout << "File size: " << size << ".\n";
	
	std::cout << "Copying file contents into memory...\n";
	
	// Go to the beginning of the file
	in.seekg(0, std::ios::beg);
	
	// Allocate memory for the buffer
	buffer = std::vector<uint8_t>(size);
	
	// Read in the file
	in.read((char*)&buffer[0], size);
	
	std::cout << "File copied into memory.\n";
	
	// Close the file
	in.close();
	
	std::cout << "Copying file to hard drive...\n";
	// Create a file at the destination
	out.open(filename[1]);
	if(!out) {std::cout << "Cannot open file.\n"; std::cin.get(); return 1;}
	
	// Write the file contents to the new file
	out.write((char*)&buffer[0], size);
	
	std::cout << "File copied.\n";
	
	std::cin.get();
	
	return 0;
}


Output:
Enter the name of the file you'd like to load: hi.txt

Where do you want the copy to end up?: hicpy.txt

Getting file parameters...
File size: 12.
Copying file contents into memory...
File copied into memory.
Copying file to hard drive...
File copied.


Also, be careful that the file size isn't too big (should be smaller than the amount of memory installed in the computer), or Windows will deny memory to the application, and the program will crash.
Last edited on
its not a good way. i want my program to be fast. How can i fix this without using this methods? its not working with photos and my other documents
Last edited on
What are you talking about? I've just tested it and it works fine with photos. Are you putting the file extension at the end of it?

What's wrong with CopyFile?
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851%28v=vs.85%29.aspx
It's platform specific.
It's platform specific.
We're in the Windows forum.

The code reads the entire file into memory placing unclear limits on the size of file that can be copied.
Last edited on
We're in the Windows forum.
Fair enough.

The code reads the entire file into memory placing unclear limits on the size of file that can be copied.

A quick modification can fix that.
i tried but when i open the jpg image that was made it says that it is corrupted! why i cant use copy file how to solve error 5 access denied? is there other faster way to accomplish this without write file to memory as described above? I prefer copyfile function instead of alternative ways.
Help!
Leonardo
Last edited on
Try using it with another jpg image.

If you want to use CopyFile still then it's because you don't have permissions. We really can't do anything to help you.
how can i get those persmissions? how to do it manually i prefer doing it using c++. note: i already and administrator account. the destination directory have read only rights. but i can delete this attribute.
Help me
Leonardo
Try right clicking and running as admin.
Last edited on
Topic archived. No new replies allowed.