Got something to say? I'd love to hear from you, but I'm afraid that if I publish my email address on a web page these days, people will write me lots of mail about how I can "increase my manhood" or purchase inexpensive V1@gra. So instead let's try this. Just put your message in, give your return address, and if you're not trying to sell me questionable goods, I'll do my best answer you. Trust me; it's so simple, even a former Nigerian general with lots of regretably inaccessible cash could do it.
#include <stdio.h>
void usage(int argc,char *argv[]) {
printf("%s: <infile> <outfile>\n",argv[0]);
printf("Copy infile to outfile, producing sparse output if possible.\n");
}
int main(int argc, char* argv[]) {
FILE *in;
FILE *out;
char buf;
int res;
if(argc < 3) {usage(argc,argv);return(1);}
if(!(in = fopen(argv[1],"rb"))) {
printf("Can't open %s for reading!\n",argv[1]);return(1);}
if(!(out = fopen(argv[2],"wb"))) {
printf("Can't open %s for writing!\n",argv[2]);return(1);}
/* One byte at a time is probably a little slow,
but will produce a perfectly compact output file */
while (res = fread(&buf,1,1,in) ) {
if (res < 1) {fflush(out);fclose(out);fclose(in);return(0);}
/* The only difference between a dense and sparse file
is that we seek instead of writing zeros. That is, in
a sparse file, the zeros are implied. */
if (buf == 0) {
res=fseek(out,1,SEEK_CUR);
if (res) {
printf("Seek error in destination\n");
return(1);
}
} else {
res=fwrite(&buf,1,1,out);
if (res<1) {
printf("Write error in destination\n");
return(1);
}
}
}
/*Can't need this, but just in case*/
fclose(in);
fflush(out);
fclose(out);
return(0);
}