2 lyq7713 lyq7713 于 2014.07.14 14:50 提问

关于文件操作函数rename与remove

我把文件指针都关闭了,可是调用rename(重命名文件)和remove(删除文件)函数都不起作用?把中间对文件的操作注释掉也一样,不知道哪里出了问题。。。两个函数的返回值都是-1,我也去查了原因,网络上讲解的好像都不是呀

1个回答

lyq7713
lyq7713   2014.07.14 14:51

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);

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!