fwrite()方法无法正常工作

fwrite() method does not work properly

本文关键字:常工作 工作 方法 fwrite      更新时间:2023-10-16

我正在尝试写入一个文件。它创建得很好,但不能什么都写。在调试模式下,我可以看到我想写的对象的所有属性。

我的方法很短,在写作之前我使用了fseek方法。

void File::add(record *ptr){
   fseek(book, 0, SEEK_END);
   fwrite(ptr, sizeof(record), 1, book);
} 

运行程序后,我检查了文件,它是空的。该文件与我的源文件是同一目录

尝试使用fputs而不是fwrite:

/* fseek example */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen ( "example.txt" , "wb" );
  fputs ( "This is an apple." , pFile );
  fseek ( pFile , 9 , SEEK_SET );
  fputs ( " sam" , pFile );
  fclose ( pFile );
  return 0;
}