C++ 删除fopen成功创建的文件时权限被拒绝
这是一个函数,它从用户处获取文件夹名称,创建一个文本文件,并在以后将其删除C++ 删除fopen成功创建的文件时权限被拒绝,c++,file,C++,File,这是一个函数,它从用户处获取文件夹名称,创建一个文本文件,并在以后将其删除 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<<
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的错误号[表示文件已成功创建。]
删除13的错误号[表示权限被拒绝]
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;
}
如您所见,该文件夹尚未成功删除。
您可能需要设置文件夹的执行权限和写入权限。您可能需要设置文件夹的执行权限和写入权限。您需要先关闭文件:
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;
}
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;
}
}
您需要先关闭该文件:
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;
}
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;
}
}
这是因为文件仍处于打开状态。您应该在删除文件夹之前将其丢失。 差不多
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;
}
void function(string LogFolder)
{
FILE *fp = fopen((LogFolder+"/test.txt").c_str(),"w");
cout<<"The errorno of fopen "<<errno<<endl;
fclose(fp);
remove((LogFolder+"/test.txt").c_str());
cout<<"The errorno of remove "<<errno<<endl;
}
这是因为文件仍处于打开状态。您应该在删除文件夹之前将其丢失。 差不多
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;
}
void function(string LogFolder)
{
FILE *fp = fopen((LogFolder+"/test.txt").c_str(),"w");
cout<<"The errorno of fopen "<<errno<<endl;
fclose(fp);
remove((LogFolder+"/test.txt").c_str());
cout<<"The errorno of remove "<<errno<<endl;
}
首先,您不是在创建和删除文件夹,而是在文件夹中创建一个名为test.txt的文件
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;
}
您的问题是,您需要先关闭该文件,然后才能允许删除它。试试下面的方法
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;
}
void function(string LogFolder)
{
FILE* myFile = fopen((LogFolder+"/test.txt").c_str(),"w");
cout<<"The errorno of fopen "<<errno<<endl;
fclose( myFile );
cout<<"The errorno of fclose "<<errno<<endl;
remove((LogFolder+"/test.txt").c_str());
cout<<"The errorno of remove "<<errno<<endl;
}
首先,您不是在创建和删除文件夹,而是在文件夹中创建一个名为test.txt的文件
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;
}
您的问题是,您需要先关闭该文件,然后才能允许删除它。试试下面的方法
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;
}
void function(string LogFolder)
{
FILE* myFile = fopen((LogFolder+"/test.txt").c_str(),"w");
cout<<"The errorno of fopen "<<errno<<endl;
fclose( myFile );
cout<<"The errorno of fclose "<<errno<<endl;
remove((LogFolder+"/test.txt").c_str());
cout<<"The errorno of remove "<<errno<<endl;
}
在尝试删除文件夹之前,您应该先关闭。除非您确实知道上一个函数失败,否则不要检查errno。如果函数没有失败,则errno的值未指定。请记住这一点。在尝试删除文件夹之前,您应该先关闭。除非您确实知道上一个函数失败,否则不要检查errno。如果函数没有失败,则errno的值未指定。请记住。工作正常。谢谢。工作正常。谢谢。