windows “删除文件时出错:权限被拒绝”在 C++ 中删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3549900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:05:46  来源:igfitidea点击:

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

c++windowsfile-io

提问by Mark Lalor

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

当我编译并运行我的 C++ 程序时,该程序会删除一个名为 example.txt 的文件(如下)

#include <stdio.h>

int main ()
{
  if( remove( "example.txt" ) != 0 )
    perror( "Error deleting 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.txton command prompt it works.

当我在del example.txt命令提示符下输入时,它可以工作。

Weird...

奇怪的...

采纳答案by Teja

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

我猜想std::remove()把路径作为参数。所以我们需要指定整个路径作为remove函数的参数。

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

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

回答by STF

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

也许您无权从 Visual Studio 代码更改此位置的文件。

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

我遇到了这个问题,我更改了文件位置,然后就没有问题了。