【问题标题】:Permission denied when renaming .dat file in C在 C 中重命名 .dat 文件时权限被拒绝
【发布时间】:2016-10-02 02:39:12
【问题描述】:

我写了这个函数来删除fguest文件的记录。它会工作几次,但经过几次尝试后它开始显示Permission denied .. 我无法弄清楚这是什么原因,谁能告诉我是什么原因?

void delResPer(void)
{
    int recFound;
    char status[50] = " ResDeleted ";

    printf("\nPlease enter Guest's ID to search: "); 
    if(scanf("%s",&search) != 1)
    {
        invalidInput();
        flushStdin();
        editRes();       
    } 
    flushStdin();

    fguest = fopen(guestFile,"rb");
    if (fguest == NULL)
    {
        printf("Unable to locate guestFile\n\n");
        returnToMain(); 
    }   
    while(fread(&grec,sizeof(GUEST),1,fguest)==1)           
    {
        if (strcmp(grec.id,search)==0)                           
        {   
            printf("\n\n\nRecord found: \n");
            printf("\nGuest\'s ID\t\t: %s",grec.id);
            printf("\nGuest\'s Name\t\t: %s",grec.name);        
            printf("\nRoom\'s Type\t\t: %d",grec.r_type);
            checked(grec.r_type);
            recFound = 1;
        }
    }


    if ( recFound != 1)
    {
        printf("No matching record found..\n");
        printf("Please retry with a valid record..\n");
        printf("Page is refreshing..");
        Sleep(500);
        editRes();
    }

    else if ( recFound == 1)
    {
        printf("\n\nDelete the record (Y/N)?");
        cfm = getchar();
        flushStdin();
        if ( cfm == 'Y' || cfm == 'y')
        {   
            ftemp = fopen(tempFile,"wb+");                              
            rewind(fguest); 
            while(fread(&grec,sizeof(grec),1,fguest)==1)    
            {
                if (strcmp(grec.id,search)!=0)                  
                {
                    fseek(ftemp,0,SEEK_CUR);                
                    fwrite(&grec,sizeof(grec),1,ftemp);     
                }
            }
            fclose(fguest);
            fclose(ftemp);
            if(remove(guestFile)!=0)
            {   
                errnum = errno;
                fprintf(stderr,"Failed to remove guestFile :: %s\n",strerror( errnum ));
                Sleep(500);
                //printf("Program is exiting..");
                //return;
            }
            if(rename(tempFile,guestFile)!=0)
            {   
                errnum = errno;
                fprintf(stderr,"Failed to rename guestFile :: %s\n",strerror( errnum ));
                Sleep(500); 
                printf("Program is exiting..");
                return;
            }
            if (check == 1 )
                SpDeluxe++;
            else if (check == 2 )
                Deluxe++;
            else if (check == 3 )
                Premium++;
            else if (check == 4 )
                Basic++;
            refreshRoomAvail();
            printf("Record is deleted successfully..\n");
            Sleep(250);
            returnToMain();
        }

        else if ( cfm == 'N' || cfm == 'n')
        {
            printf("Deletion of record is cancelled\n");
            printf("Page is refreshing..");
            Sleep(500);
            editRes();                                      
        }

        else
        {
            invalidInput();
            editRes();                                          
        }
    }                                                   
}

【问题讨论】:

    标签: c file error-handling permission-denied


    【解决方案1】:

    您的文件句柄用完了。

    fguest 在函数的顶层打开,但只在 cfm == 'Y' || cfm == 'y' 的分支中关闭。如果不满足该条件,或者从未达到该代码(例如,因为recFound != 1),则文件保持打开状态。

    您需要在函数退出之前始终关闭文件。

    【讨论】:

    • 哦,我明白了!在我将 fclose(fguest) 添加到 recFound!=1cfm == 'n' || cfm == 'N'else 后,它现在可以正常工作了@
    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    相关资源
    最近更新 更多