开发者

"Error deleting file: Permission denied" with remove in C++

开发者 https://www.devze.com 2023-01-12 14:08 出处:网络
When I compile and run my C++ program that deletes a file called example.txt (below) #include <stdio.h>

When I compile and run my C++ program that deletes a file called example.txt (below)

#include <stdio.h>

int main ()
{
  if( remove( "example.txt" ) != 0 )
    perror( "Error deleti开发者_如何学Pythonng file" );
  else
    puts( "File successfully deleted" );
  return 0;
}

It comes out like this...

cd c:\Users\Mark\Desktop  
C:\Users\Mark\Desktop>app.exe  
Error deleting file: Permission denied  

I lifted all restrictions on the file and there is full access to anyone (that should include my program).

Any solutions?

EDIT

When I type in del example.txt on command prompt it works.

Weird...


I guess std::remove() takes the path as a parameter. So we need to specify the entire path as a parameter for remove function.

eg: remove("home/xxx/example.txt");


Maybe you don't have permission to change files in this place from your visual studio code.

I had this problem and I changed the file location and than I had no more problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消