[英]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.