我正在將結構保存到.dat文件中。讓我們假設我必須編輯一個特定的結構,我將如何繼續?我做了以下內容:如何使用C覆蓋文件中的結構?
ptFile = fopen("funcionarios.dat", "ab+");
fseek(ptFile, index*sizeof(strFunc), SEEK_SET); //places the pointer at the struct I want
fwrite(&newStruct, sizeof(strFunc), 1, ptFile); //adds the new struct
因此,如您所見,我想用newStruct更新我的文件。
fwrite函數返回1,但它不會替換我想要的行(如果我使用了錯過的索引,也不會替換鄰居行),並且它不會將新結構添加到文件中。它什麼都不做!
任何想法?
我通過閱讀所有的結構,用我的newStruct替換索引結構並用所有結構編寫文件來完成工作,但我正在尋找更好的方法來實現這一目標。
在此先感謝。
也許使用的,而不是AB + W +? –
使用W +我不會擦除整個文件? –