
SUMMARY: "Deleting every other byte"
Summary: "Problem deleting every other byte of speech sample"
(1) single small buffer (my first solution):
#define BUFSIZE 2
char buf[BUFSIZE];
while(!feof(infile))
{
fread(buf, sizeof(char), 2, infile);
fwrite(buf, sizeof(char), 1, outfile);
}
(2) twin buffers (suggested by Lars Wirzenius and several others):
#define BUF_MAX 2048
char inbuf[BUF_MAX], outbuf[BUF_MAX];
while ((n = fread(inbuf, 1, sizeof(inbuf), infile)) > 0)
{
for (i = 0; i < n/2; ++i)
outbuf[i] = inbuf[i*2];
fwrite(outbuf, 1, n/2, outfile);
}
#define BUF_MAX 4096
(3) twin buffers using read() and write() (suggested by several for
Unix systems):
char inbuf[BUF_MAX], outbuf[BUF_MAX];
int ifhandle, ofhandle, bytes, i;
while((bytes = read(ifhandle, inbuf, BUF_MAX)) > 0)
{
for(i=0; i<bytes/2; ++i)
outbuf[i] = inbuf[i*2];
write(ofhandle, outbuf, bytes/2);
}
(4) direct method using getc and putc (suggested by Joe Wright; Joe
Pop noted that if getc and putc are not macros there will be an
overhead of unnecessary function calls):
FILE *infile, *outfile;
int i = 0;
int b;
while ((b = getc(infile)) != EOF)
/*bits in an int are numbered 0-15 or 0-31 from right to left*/
if (++i & 1) /*if bit 0 of 1st operand and bit 0 of 2nd are 1, get 1*/
putc(b, outfile);
I tested these four methods on a 1.2 Mb file. The reduction to a
560 Kb file on a small Xenix 386 system took:
(1) 55 secs
(2) 22
(3) 21
(4) 25
Getc and putc are defined as macros in the Xenix C compiler library
(2.3.1b).
Thanks to all who helped!
Chet Creider