rem: file = fopen(filename, "w");
add: file = fopen(filename, "wb");
context: static gboolean DownloadMemory(const char* filename, unsigned long start, unsigned long length) {#define FBLOCKSZ (64*1024) //64k.D  unsigned t,i,count,tcount;  int data;  char monitorcmd[STRINGSIZE];  FILE* file = NULL;  char *buffer;    unsigned int fblocksz=1;	//The buffer size is softcoded so we can resize it				//for the first transfer (cam bug workaround) and for the last				//partial block transfer.     char Tmpfile[255];    buffer=malloc(FBLOCKSZ);  if(buffer==NULL)    {      Log("Couldn't allocate transfer buffer");      return FALSE;    }    //first make sure we're on the right partition and directory!  ChangePartition(0);  ChangeDirectory("/DCIM");    //  if (file.Open((LPCSTR)filename, CFile::modeCreate | CFile::modeWrite)==false)  file = fopen(filename, "w");  if (file == NULL) {    Log("Trouble creating: ");    Log(filename);    free(buffer);    return FALSE;  }      i=0;  for(t=start;t<start+length;t=t+fblocksz) {    fblocksz=FBLOCKSZ;        if(t+FBLOCKSZ>start+length) //just download the last portion of a block      fblocksz=(start+length)-t;          if(t==0)      fblocksz=4; //we always short a transfer that starts at 0 to    //workaround a cam bug.        i++;    memset(Tmpfile,0,255);    sprintf(Tmpfile,"%07x.BIN",1);    snprintf(monitorcmd, STRINGSIZE-1, "dumpf %u %u %s", t, fblocksz, Tmpfile);    //    monitorcmd.Format("dumpf %d %d %s",t,fblocksz,Tmpfile);    //		Log(monitorcmd);    if(Monitor(monitorcmd)==FALSE) {      Log("flash dump command failed: ");      Log(monitorcmd);      fclose(file);      free(buffer);      return FALSE;    }          if(ControlMessageWrite(0xb101, Tmpfile, strlen(Tmpfile)+1, TIMEOUT)==FALSE) { // SetFileName             Log("failed at 0xb1");      return FALSE;    }                  data=0x00;    if(ControlMessageWrite(0x9301, (char*)&data, 0, TIMEOUT)==FALSE) { // Request File Read      Log("failed at 0x93");      return FALSE;    }        tcount=0;    set_bitrate(t);    while (1) {      count=Read(buffer,BUFSIZE,TIMEOUT);      if(count<1)	break;      tcount+=count;      //file.WriteHuge(buffer,count);      fwrite(buffer,sizeof(char), count, file);            if(count<BUFSIZE)	break;      //DoMessagePump();    }          if(tcount<fblocksz) {      Log("short buffer error. data will likely be corrupt");      free(buffer);      fclose(file);      return FALSE;    }                //      DoMessagePump();        }  fclose(file);  Log("memory download succeeded");  free(buffer);  return TRUE;}