我有这个问题。我有一个 C++ 程序;该程序成功地制作了我保存记录的文件。在一个过程中,我编辑了一条记录并制作了另一个具有不同名称的文件。最后我关闭了这两个文件,当我尝试删除旧文件并重命名新文件时,出现此错误:
删除文件时出错:权限被拒绝。
void SoldDevices()
{
int soldQuantity = 0;
char soldModel[20];
ElShop tempVar;
FILE *newFile;
printf("Enter model of sold device: ");
gets(soldModel);
file = fopen(fileName, "r+");
fread(&shop, sizeof(shop), 1, file);
while (!feof(file))
{
if (strcmp(shop.model, soldModel) == 0)
{
tempVar = shop;
break;
}
fread(&shop, sizeof(shop), 1, file);
}
fclose(file);
printf("Enter how much devices are sold: ");
scanf("%d", &soldQuantity);
while (tempVar.quantity < soldQuantity)
{
printf("No items available!\n");
printf("Enter how much devices are sold: ");
scanf("%d", &soldQuantity);
}
tempVar.quantity = tempVar.quantity - soldQuantity;
printf("%d\n", tempVar.quantity);
file = fopen(fileName, "rb");
newFile = fopen("New", "wb");
fread(&shop, sizeof(shop), 1, file);
while (!feof(file))
{
if(strcmp(soldModel, shop.model) == 0)
{
fwrite(&tempVar, sizeof(shop), 1, newFile);
}
else
{
fwrite(&shop, sizeof(shop), 1, newFile);
}
fread(&shop, sizeof(shop), 1, file);
}
fclose(newFile);
fclose(file);
if( remove( fileName ) != 0 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );
rename("New", fileName);
}
有没有人有一些想法来解决这个问题?