[英]Why remove and rename functions does not work in my program?
当我尝试使用繁琐的代码行删除文件时,结果是两个文件(stock.dat和clone.dat)在程序结束后都存在(结果我只想存在重命名的stock)。 dat(=原始名称为clone.dat))。 提前致谢。
int code;
FILE *stock=fopen("stock.dat","rb");
FILE *stc_clone=fopen("clone.dat","wb");
printf("PLEASE TYPE THE CODE OF THE PRODUCT YOU WISH TO DELETE:\t");
scanf(" %d",& code);
printf("\n");
fseek(stock,0,SEEK_END);
int fl_size=ftell(stock);
int quantity= fl_size/sizeof(product);
rewind(stock);
prdct prd_pntr= (product *) malloc(sizeof(product)*quantity);
assert(prd_pntr);
fread(prd_pntr,sizeof(product),quantity,stock);
int i;
for(i=0;i<quantity;i++){
if(prd_pntr[i].code==code){
continue;
}else{
fprintf(stc_clone,"%d %s %d",prd_pntr[i].code,prd_pntr[i].description,prd_pntr[i].volume);
}
}
fclose(stc_clone);
fclose(stock);
remove(stock);
rename("clone.dat","stock.dat");
free(prd_pntr);
printf("\n\a THE PRODUCT DELETED!!!\n");
您的函数不会删除文件,因为您正在将FILE*
而不是char*
传递给remove
函数:
FILE *stock = fopen("stock.dat","rb");
...
fclose(stock);
remove(stock); // <<== HERE: you are supposed to pass a name, not FILE*
要解决此问题,请按如下所示更改remove
行:L
remove("stock.dat");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.