[英]Why remove and rename functions does not work in my program?
When I try to delete a file using the fowling lines of code, I get as result that the both files (stock.dat and clone.dat) exists after the end of the program (as result i want to exist only the renamed stock.dat (=original name was clone.dat)). 当我尝试使用繁琐的代码行删除文件时,结果是两个文件(stock.dat和clone.dat)在程序结束后都存在(结果我只想存在重命名的stock)。 dat(=原始名称为clone.dat))。 Thanks in advance.
提前致谢。
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");
Your function does not remove the file because you are passing FILE*
instead of char*
to the remove
function: 您的函数不会删除文件,因为您正在将
FILE*
而不是char*
传递给remove
函数:
FILE *stock = fopen("stock.dat","rb");
...
fclose(stock);
remove(stock); // <<== HERE: you are supposed to pass a name, not FILE*
To fix the problem, change the remove
line as followsL 要解决此问题,请按如下所示更改
remove
行:L
remove("stock.dat");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.