Advertisement

text ok, binary nope

Started by November 14, 2000 07:21 AM
1 comment, last by soehrimnir 24 years, 1 month ago
I''ve got this little function that does a simple encryption with a key. Here it is:
  
void encrypt(void) {
  char cryptkey[] = "OkD9dal13lfjf893l*7mf9iLdfm90q";
  int i=0;
  FILE *ifilep, *ofilep;
  char c;

  if ((ifilep=fopen("test.zip","rb")) == NULL) {
    printf("Couldn''t load input file.");
    exit(1);
  }
  if ((ofilep=fopen("encrypt.dat","wb")) == NULL) {
    printf("Couldn''t create output file.");
    exit(1);
  }

  while ((c=getc(ifilep)) != EOF) {
    c = c ^ cryptkey[i++];
    fputc(c,ofilep);
    if (i==30) i=0;
  }

  fclose(ofilep); fclose(ifilep);
}
  
Know when I test this on a text file, it works fine. But when I try to do it with a binary it only writes a few bytes to the file, even when the input file is about 50k. Does anyone know what the problem is?
Although I can''t confirm this, I suspect the getc/putc functions are intended for text files. With binary files, I would use fread/fwrite.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
---- --- -- -
New York. New York. New York. Texas. Texas. New York. New York. Canada.
---- --- -- -
Advertisement
yep, it is, thanks mossmoss

This topic is closed to new replies.

Advertisement