Return to Snippet

Revision: 14043
at May 16, 2009 18:02 by Zufolek


Initial Code
void*loadfile(const char*const s){
 FILE*f=fopen(s,"rb");
 if(f){
  fseek(f,0,SEEK_END);
  {
   const int ml=ftell(f);
   if(ml){
    char*m=malloc(ml);
    if(m){
     rewind(f);
     fread(m,1,ml,f);
     fclose(f);
     return m;
    }
   }
  }
 }
 return 0;
}


int savefile(const char*const s,const void*const m,const int ml){
 FILE*f=fopen(s,"wb");
 if(f){
  int ok=fwrite(m,1,ml,f)==ml;
  fclose(f);
  return ok;
 }
 return 0;
}

Initial URL


Initial Description
loadfile(filename);
returns NULL on fail or a pointer that should be free()d when no longer needed.

savefile(filename,data,dataSize);
returns 0 on fail or 1 on success.

Initial Title
load or save a chunk of memory to/from file

Initial Tags
file, load

Initial Language
C