拾遗笔记

读写二进制文件,读写int

//file name : a.c
//created at: 2011年11月01日 星期二 20时28分13秒

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

int main(int argc, char *argv[]){
  FILE *fp = fopen("/tmp/aout", "wb+");
  int a =30;
  fwrite(&a,sizeof(int),1,fp);
  a=1;
  fwrite(&a,sizeof(int),1,fp);
  fclose(fp);

  fp = fopen("/tmp/aout", "rb");

  fseek(fp,0,SEEK_SET);
  fread(&a,sizeof(int),1,fp);
  printf ("%d\n",a);

  fseek(fp,0+sizeof(int),SEEK_SET);
  fread(&a,sizeof(int),1,fp);
  printf ("%d\n",a);

  return 0;
}

Comments

comments powered by Disqus