开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜