写了个程序,要删除E:\hugo.txt,确保文件已关闭,不是只可读,为什么仍然出现了permission denied,该怎么解决
重来一遍
int PrintOderpage()
{
char templater[100];
int moveway;
FILE *fp,*fp2;
//文件读写准备
char path[]="E:\\";
strcat(path,user.userName);//user.userName为hugo
strcat(path,".txt");
fp=fopen(path,"r+");//读写形式打开文件
int ch;
int i=0;
while(1)
{
ch=fgetc(fp);
if(ch=='#')
{
break;
}
}
ch=fgetc(fp);
while(1)//文件里空格代表密码结束
{
user.keyWord[i]=ch;
i++;
ch=fgetc(fp);
if(ch==' ')
break;
}
user.keyWord[i]='\0'; //从文件读取用户密码
fclose(fp);
fp=fopen(path,"r+");//读写形式打开文件
fscanf(fp,"%s",user.date);//此行代码为了跳过第一行
fscanf(fp,"%s",user.date);//此行代码为了跳过第一行
fscanf(fp,"%s",user.sex);
fscanf(fp,"%s",user.date);//此行过后指针指向购物信息的那一行
system("cls");
printf(">>>您的订单信息为:\n");
printf(">>>用户名: %s 用户性别: %s 用户生日: %s \n",user.userName,user.sex,user.date);
moveway=ftell(fp);//文件偏移36
fclose(fp);
system("pause");
i=0;
int price=0;
while(i>>商品总价 : %d元\n",price);
char chioce;
printf(">>>您确认付款吗?(y/n):");
scanf(" %c",&chioce);
char keyword[15];
int flag=0;
//重写文件
fp2=fopen("E:\\temp.txt","w+");//创建临时文件
if(fp==NULL)
{
exit(0);
}
fp=fopen(path,"r");
while(flag==0)
{
switch(chioce)
{
case 'y':
case 'Y':
i=0;
while(1)
{
printf(">>>请输入用户%s的密码:",user.userName);
i=0;
char k;
while((k=getch())!='\r')
{
keyword[i]=k;
if(keyword[i]=='\b')
{
printf("\b \b");
i--;
}
else
{
printf("*");
i++;
}
}
keyword[i]='\0';
i=0;//下标归零
if(strcmp(user.keyWord,keyword)==0)
{
printf(">>>订单打印中......\n");
Sleep(1000);
fread(templater,moveway,1,fp);//将购物信息之前的信息全部转移到temp文件
for(i=0;i<35;i++)//重写用户基本信息
{
fprintf(fp2,"%c",templater[i]);
}
i=0;
while(i>>订单打印成功\n");
system("pause");
flag=1;
break;
}
else
{
printf(">>>密码输入错误!请重新输入\n");
continue;
}
}
break;
case 'n':
case 'N':
flag=1;
break;
default:
printf(">>>输入错误,请重新输入\n");
continue;
}
}
fclose(fp);
fclose(fp2);
if(remove("E:\\hugo.txt")==0)
{
printf("remove success");
}
else
{
perror("remove");
}
rename("E:\\temp.txt","E:\\hugo.txt");
}
解决方案2:重来一遍
int PrintOderpage()
{
char templater[100];
int moveway;
FILE *fp,*fp2;
//文件读写准备
char path[]="E:\\";
strcat(path,user.userName);//user.userName为hugo
strcat(path,".txt");
fp=fopen(path,"r+");//读写形式打开文件
int ch;
int i=0;
while(1)
{
ch=fgetc(fp);
if(ch=='#')
{
break;
}
}
ch=fgetc(fp);
while(1)//文件里空格代表密码结束
{
user.keyWord[i]=ch;
i++;
ch=fgetc(fp);
if(ch==' ')
break;
}
user.keyWord[i]='\0'; //从文件读取用户密码
fclose(fp);
fp=fopen(path,"r+");//读写形式打开文件
fscanf(fp,"%s",user.date);//此行代码为了跳过第一行
fscanf(fp,"%s",user.date);//此行代码为了跳过第一行
fscanf(fp,"%s",user.sex);
fscanf(fp,"%s",user.date);//此行过后指针指向购物信息的那一行
system("cls");
printf(">>>您的订单信息为:\n");
printf(">>>用户名: %s 用户性别: %s 用户生日: %s \n",user.userName,user.sex,user.date);
moveway=ftell(fp);//文件偏移36
fclose(fp);
system("pause");
i=0;
int price=0;
while(i>>商品总价 : %d元\n",price);
char chioce;
printf(">>>您确认付款吗?(y/n):");
scanf(" %c",&chioce);
char keyword[15];
int flag=0;
//重写文件
fp2=fopen("E:\\temp.txt","w+");//创建临时文件
if(fp==NULL)
{
exit(0);
}
fp=fopen(path,"r");
while(flag==0)
{
switch(chioce)
{
case 'y':
case 'Y':
i=0;
while(1)
{
printf(">>>请输入用户%s的密码:",user.userName);
i=0;
char k;
while((k=getch())!='\r')
{
keyword[i]=k;
if(keyword[i]=='\b')
{
printf("\b \b");
i--;
}
else
{
printf("*");
i++;
}
}
keyword[i]='\0';
i=0;//下标归零
if(strcmp(user.keyWord,keyword)==0)
{
printf(">>>订单打印中......\n");
Sleep(1000);
fread(templater,moveway,1,fp);//将购物信息之前的信息全部转移到temp文件
for(i=0;i<35;i++)//重写用户基本信息
{
fprintf(fp2,"%c",templater[i]);
}
i=0;
while(i>>订单打印成功\n");
system("pause");
flag=1;
break;
}
else
{
printf(">>>密码输入错误!请重新输入\n");
continue;
}
}
break;
case 'n':
case 'N':
flag=1;
break;
default:
printf(">>>输入错误,请重新输入\n");
continue;
}
}
fclose(fp);
fclose(fp2);
if(remove("E:\\hugo.txt")==0)
{
printf("remove success");
}
else
{
perror("remove");
}
rename("E:\\temp.txt","E:\\hugo.txt");
}
解决方案3:代码:
int PrintOderpage()
{
char templater[100];
int moveway;
FILE *fp,*fp2;
//文件读写准备
char path[]="E:\";
strcat(path,user.userName);//user.userName为hugo
strcat(path,".txt");
fp=fopen(path,"r+");//读写形式打开文件
int ch;
int i=0;
while(1)
{
ch=fgetc(fp);
if(ch=='#')
{
break;
}
}
ch=fgetc(fp);
while(1)//文件里空格代表密码结束
{
user.keyWord[i]=ch;
i++;
ch=fgetc(fp);
if(ch==' ')
break;
}
user.keyWord[i]='\0'; //从文件读取用户密码
fclose(fp);
fp=fopen(path,"r+");//读写形式打开文件
fscanf(fp,"%s",user.date);//此行代码为了跳过第一行
fscanf(fp,"%s",user.date);//此行代码为了跳过第一行
fscanf(fp,"%s",user.sex);
fscanf(fp,"%s",user.date);//此行过后指针指向购物信息的那一行
system("cls");
printf(">>>您的订单信息为:\n");
printf(">>>用户名: %s 用户性别: %s 用户生日: %s \n",user.userName,user.sex,user.date);
moveway=ftell(fp);//文件偏移36
fclose(fp);
system("pause");
i=0;
int price=0;
while(i>>商品总价 : %d元\n",price);
char chioce;
printf(">>>您确认付款吗?(y/n):");
scanf(" %c",&chioce);
char keyword[15];
int flag=0;
//重写文件
fp2=fopen("E:\\temp.txt","w+");//创建临时文件
if(fp==NULL)
{
exit(0);
}
fp=fopen(path,"r");
while(flag==0)
{
switch(chioce)
{
case 'y':
case 'Y':
i=0;
while(1)
{
printf(">>>请输入用户%s的密码:",user.userName);
i=0;
char k;
while((k=getch())!='\r')
{
keyword[i]=k;
if(keyword[i]=='\b')
{
printf("\b \b");
i--;
}
else
{
printf("*");
i++;
}
}
keyword[i]='\0';
i=0;//下标归零
if(strcmp(user.keyWord,keyword)==0)
{
printf(">>>订单打印中......\n");
Sleep(1000);
fread(templater,moveway,1,fp);//将购物信息之前的信息全部转移到temp文件
for(i=0;i<35;i++)//重写用户基本信息
{
fprintf(fp2,"%c",templater[i]);
}
i=0;
while(i>>订单打印成功\n");
system("pause");
flag=1;
break;
}
else
{
printf(">>>密码输入错误!请重新输入\n");
continue;
}
}
break;
case 'n':
case 'N':
flag=1;
break;
default:
printf(">>>输入错误,请重新输入\n");
continue;
}
}
fclose(fp);
fclose(fp2);
if(remove("E:\\hugo.txt")==0)
{
printf("remove success");
}
else
{
perror("remove");
}
rename("E:\\temp.txt","E:\\hugo.txt");
}