这是一个函数,它从用户那里获取文件夹名称,创建一个文本文件,稍后应该将其删除。
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/