rem: FILE* file = fopen(filename, "w");
add: FILE* file = fopen(filename, "wb");
context: gboolean DownloadFlash(const char* filename, int partition, int size) {  //Ok, this one's pretty ugly... The problem is that the firmware  //doesn't want to send a file who's sector count isn't divisible  //by the clusters-per-sector value. So if the partition size isn't  //evenly divided by sectors divided by clusters, then we have to  //pad it, and then throw away the padding.  //...it could be something else, but assuming the above fixes  //a "rounded down" bug when delivering the NO_NAME partition.    unsigned char buffer[BUFSIZE];  int data[3];  int dummy;  int i,x;  int roundedsize,bufsize;      FILE* file = fopen(filename, "w");  if (file == NULL) {    Log("Trouble creating filename");    return FALSE;  }  if(partition==0) { //we repurpose 0 to mean start at 3 and grab the rest of the flash        partition=3;    size=(128*1024*1024); //set bigger than the maximum we can grab    roundedsize=size;  }  if(partition==1) {    size=(32*1024*1024); //p1 appears to be a ramdisk or something 32 Meg in size.    roundedsize=size;  } else if(size==-1) { //they haven't requested a size, get the right size for this partition        size=GetPartitionSize(partition, FALSE);    roundedsize=GetPartitionSize(partition,TRUE);    if(size<1)      return FALSE;  } else    roundedsize=size;  data[0]=0;  data[1]=roundedsize;  if(partition==2) {    //skip the first 512 bytes (the EBR) by setting data[0]=512    data[0]=512;  }    /*  p_ctl_progress->SetRange32(0,(roundedsize/BUFSIZE)/32);  p_ctl_progress->SetStep(1);  p_ctl_progress->SetPos(0);*/    if(ControlMessageWrite(0xf100|partition,data,8,TIMEOUT)==FALSE) { // Set memory transfer filter, and size    Log("failed at 0xf1, set memory type");    return FALSE;  }  if(ControlMessageWrite(0xf300,&dummy,0,TIMEOUT)==FALSE) { // Initiate the memory transfer    Log("failed at 0xf3, request memory");    return FALSE;  }    bufsize=BUFSIZE;  int count=0;  for(i=0;;i++) {    memset(buffer,0,BUFSIZE);    x=Read((unsigned char *)buffer,bufsize,TIMEOUT);    if(x>0) {      //      file.WriteHuge(buffer,(DWORD)x);      fwrite(buffer, sizeof(char), x, file);      count+=x;      if(count+bufsize>size) { //we gotta shrink the buffer	bufsize=size-count;      }    }        if(i%32==0) {      set_progress_bar((double)count / (double)size);      // p_ctl_progress->SendMessage(PBM_SETBARCOLOR,0,(LPARAM)RGB(0,0,220)); //clear the text      //p_ctl_progress->StepIt();    }    set_bitrate(count);    //  DoMessagePump();    if((x<bufsize)||(count==size))      break;  }  //p_ctl_progress->SetPos(0);  if(roundedsize!=size) { //we need to bleed off the excess requested bytes.        for(;;) {      x=Read((unsigned char *)buffer,BUFSIZE,TIMEOUT); //applying leeches      if(x<1)	break;    }  }    fclose(file);    //CString debug; debug.Format("flash download was %d bytes",count);  fprintf(stderr, "flash download was %d bytes\n",count);  //  Log(debug);  return TRUE;}