内存数据保存到文件

#include <stdio.h>
#include <string.h>
int main( void )
{
 FILE *stream;
 typedef struct _tt{
  int a;
  int b;
  char buf[20];
 }tt;
 tt temp;
 temp.a=10;
 temp.b=20;
 strcpy(temp.buf,"hello");
 
 int c=sizeof(temp);
 stream= fopen("at.dat","w+");
 fwrite(&temp,sizeof(temp),1,stream);
 
 char *pbuf=new char[sizeof(temp)];
 
 fseek(stream,0,SEEK_SET);
 fread(pbuf,sizeof(temp),1,stream);
    fclose(stream);
    tt *p=(tt*)pbuf;
 printf("%d %d %s",p->a,p->b,p->buf);
}
posted @ 2007-10-23 15:33  ahuo  阅读(691)  评论(0编辑  收藏  举报