yy-chr: set file size

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
yy-chr: set file size
by on (#175331)
I noticed that some chr files are different sizes (some are 8kb, some are 2kb...) and I need mine to be 1kb in size, but whenever I create a new file in YY-CHR it's 8KB large by default. How can I change this?
Re: yy-chr: set file size
by on (#175333)
AFAIK it's not possible. You can edit smaller files though (you can use a hex editor like HxD to create a file full of zeros). Caution: If you make changes in areas that don't fall within the original size of the file, the changes won't be saved.
Re: yy-chr: set file size
by on (#175336)
That works, thanks!
Re: yy-chr: set file size
by on (#177300)
Because Tokumaru's tile compressor takes a max of 4kb of tile data I wrote a simple C program that carves up the standard 8kb chr file for myself. Here's an example program for you that fits your needs:

Code:
#include <stdio.h>
#include <stdlib.h>
char* extractKB(char* indata,int size){
   char* cut = malloc(size);
   
   memcpy(cut,indata,size);   //copy first kb
   
   return cut;
}

int getFileSize(FILE* fp){
   int count = 0;
   while(fgetc(fp)!=EOF)
      count++;
   fseek(fp,0,0);
   return count;
}


int main(int argc,char** argv) {
   if(argc < 3)
      return 1;
   FILE* fp = fopen(argv[1],"rb");
   int size = getFileSize(fp);
   char* buffer=  malloc(size);
   fread(buffer,1,size,fp);
   fclose(fp);
   
   fp = fopen(argv[2],"wb");
   char* extracted = extractKB(buffer,1024);
   
   fwrite(extracted,1,1024,fp);
   
   fclose(fp);
   
   free(buffer);
   free(extracted);
   return 0;
}


It takes two command line args, the chr file and then the output file.
Re: yy-chr: set file size
by on (#177310)
You can't be serious with that getFileSize() routine, are you? You don't need the while/fgetc at all. Try:

Code:
long getFileSize(FILE *fp) {
  long count;

  fseek(fp, 0, SEEK_END);
  /* TODO handle when ftell() returns -1 (error; errno is set) */
  count = ftell(fp);
  fseek(fp, 0, SEEK_SET);

  return count;
}

Note that ftell() returns long, not int, so other portions of the program will need to be updated. Also, fread() takes a size_t for its size and member counts, so you might have to force-cast (no guarantee size_t is a long on every arch).

I'm intentionally choosing not to comment on the rest. :-)
Re: yy-chr: set file size
by on (#177315)
Python is quite a bit more practical for tasks like that:
Code:
import sys; open( sys.argv[2], "wb" ).write( open( sys.argv[1], "rb" ).read( 1024 ) )
(That's the whole program.)

On *nix you could achieve the same thing with dd.
Re: yy-chr: set file size
by on (#177395)
koitsu wrote:
You can't be serious with that getFileSize() routine, are you? You don't need the while/fgetc at all. Try:

Code:
long getFileSize(FILE *fp) {
  long count;

  fseek(fp, 0, SEEK_END);
  /* TODO handle when ftell() returns -1 (error; errno is set) */
  count = ftell(fp);
  fseek(fp, 0, SEEK_SET);

  return count;
}

Note that ftell() returns long, not int, so other portions of the program will need to be updated. Also, fread() takes a size_t for its size and member counts, so you might have to force-cast (no guarantee size_t is a long on every arch).

I'm intentionally choosing not to comment on the rest. :-)


That's okay, critique is welcome. I'm certainly not the Da Vinci of C code :lol: (slapped it together on the way home from work).
Re: yy-chr: set file size
by on (#177396)
thefox wrote:
On *nix you could achieve the same thing with dd.

There is dd for Windows (which also implements /dev/null, /dev/zero, and /dev/random "devices" for convenience). I've used this for a long time with great success.