c语言 文件 remove()函数出现permission denied

c语言 文件 remove()函数出现permission denied

问题描述:

写了个程序,要删除E:\hugo.txt,确保文件已关闭,不是只可读,为什么仍然出现了permission denied,该怎么解决

 char path[]="E:\\";
strcat(path,user.userName);//user.userName为hugo
strcat(path,".txt");

你这样写会造成越界的,是不是覆盖到了什么,建议楼主单步调试,查看每一步的返回值
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

你确信这个文件确实存在吗?是不是路径写错了,楼主最好贴点代码
你确定你是否有删除该文件的权限?
你确定这个文件在你的程序的另一个部分没有打开吗?

重来一遍

 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<BUYCAR.lenth)//计算总价
    {
        price+=BUYCAR.good[i].price;
        i++;
    }
    printf("************************************************\n");
    printf(">>>商品总价 : %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<BUYCAR.lenth)
                        {
                            fprintf(fp2,"%s %f %d\n",BUYCAR.good[i].name,BUYCAR.good[i].price,BUYCAR.good[i].buy);
                            printf("%s %f %d\n",BUYCAR.good[i].name,BUYCAR.good[i].price,BUYCAR.good[i].buy);
                            i++;
                        }
                        printf(">>>订单打印成功\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");


}