2 lyq7713 lyq7713 于 2014.07.15 15:00 提问

调用文件重命名函数rename失败,errno的值为17,如何解决?

void onMenuDele(char *id)
{
FILE *p=fopen("f:\employee.txt","r"); //打开文件
if(p==NULL)
{
printf("Sorry!employee.txt cannot open!\n");
return ;
}

FILE *fp2=fopen("f:\\employ.txt","w+");     //创建临时文件
if(fp2==NULL)
{
    printf("Sorry!employ.txt cannot open!\n");
    return ;
}

char s[68];
char t[4],*q=id;

while(!feof(p))
{  
    fgets(s,68,p);
    if(!feof(p))
    {
        int count=0;
        id=q;

        for(int i=3,j=0;i<=6,j<4;i++,j++)
        {
            t[j]=s[i];
            if(t[j]==*id)
            {
                id++;
                count++;
            }
        }

        if(count!=4)
            fprintf(fp2,"%s",s);
    }
} 

fclose(p);
fclose(fp2);

/*if(remove("f:\\employee.txt")==0)
    printf("The file has been deleted!\n");
else
    printf("Sorry!The file cannot be deleted!\n");

if(rename("f:\\employ.txt","f:\\employee.txt")==0)
    printf("The file has been renamed!\n");
else
    printf("Sorry!The file cannot be renamed!\n");*/
int re_remove=remove("f:\\employee.txt");
int re_rename=rename("f:\\employ.txt","f:\\employee.txt");
printf("re_remove=%d",re_remove);
printf("re_rename=%d",re_rename);

int errno_t=errno;
printf("errno=%d",errno_t);

}

Csdn user default icon
上传中...
上传图片
插入图片