-2
我正在使用fread進行文件讀取,如下面的代碼所示。請幫助我如何立即刪除我完成閱讀的文件的內容。因此,如果我讀取一個值,請從文件中刪除該值並執行fclose。稍後當我打開文件時,文件應該只剩下兩個值。 我不知道用哪個函數來刪除。請幫幫我。 如何同時讀取和刪除文件中的內容C
我使用下面
#include<stdio.h>
#include <stdlib.h>
int main()
{
int *p;
p = malloc(sizeof(int));
p1 = malloc(sizeof(int));
p2 = malloc(sizeof(int));
*p = 5;
*p1 = 6;
*p2 = 7;
FILE *fp1 = fopen("sridhar.txt","w");
fwrite(p,sizeof(int),1,fp1);
fwrite(p1,sizeof(int),1,fp1);
fwrite(p2,sizeof(int),1,fp1);
fclose(fp1);
printf("\n Value of p p1 p2 written into the file is :%d \n",*p,*p1,*p2);
int *q, *q1, *q2;
FILE *fp2 = fopen("sridhar.txt","r");
q = malloc(sizeof(int));
q1 = malloc(sizeof(int));
q2 = malloc(sizeof(int));
fread(q,sizeof(int),1,fp2);
fclose(fp2); // Delete Logic may come here
*fp2 = fopen("sridhar.txt","r");
fread(q1,sizeof(int),1,fp2);
fclose(fp2);
// Delete Logic In here
*fp2 = fopen("sridhar.txt","r");
fread(q2,sizeof(int),1,fp2);
fclose(fp2);
// Delete logic here
printf("\n Value of q q1 q2 read from the file is :%d %d %d\n",*q, *q1, *q2);
return 0;
}
現在的問題是:什麼是你的_delete_。是否足以用空白或零字節覆蓋它?還是必須徹底刪除它,即後續的內容被轉移(這更乏味)。 – Ctx
不相關,但'int * q; q = malloc(sizeof(int)); ..。 fread(q,...'是無意義的,使用int q; ... fread(&q,...'而不是 –
...和'* fp2 = fopen(「sridhar.txt」,「r」) ;'應該是'fp2 = fopen(「sridhar.txt」,「r」);'。編譯時啓用所有警告 –