gpt4 book ai didi

c++ - 删除 fopen 成功创建的文件时权限被拒绝

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:32 25 4
gpt4 key购买 nike

这是一个函数,它从用户那里获取文件夹名称,创建一个文本文件,稍后应该将其删除。

void function(string LogFolder)
{
fopen((LogFolder+"/test.txt").c_str(),"w");
cout<<"The errorno of fopen "<<errno<<endl;
remove((LogFolder+"/test.txt").c_str());
cout<<"The errorno of remove "<<errno<<endl;
}

输出:
fopen 0的errorno【说明文件创建成功】
remove 13的errorno[表示Permission denied]

如您所见,该文件夹被成功删除。
A link to understand error codes

最佳答案

您需要先关闭文件:

void function(string LogFolder)
{
// Acquire file handle
FILE* fp = fopen((LogFolder+"/test.txt").c_str(),"w");

if(fp == NULL) // check if file creation failed
{
cout<<"The errorno of fopen "<<errno<<endl;
return;
}

// close opened file
if(fclose(fp) != 0)
{
cout<<"The errorno of fclose "<<errno<<endl;
}


if(remove((LogFolder+"/test.txt").c_str()) != 0)
{
cout<<"The errorno of remove "<<errno<<endl;
}
}

关于c++ - 删除 fopen 成功创建的文件时权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20237676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com