I must have needed something like "dd" for Dos, so I wrote this. It only allows specification of the number of bytes to skip and the total number of bytes to copy before quitting. I can't remember what I needed this for, but I do remember using it frequently.
#include <stdio.h> char buf[4096]; main(int argc, char **argv) { long skip=0; unsigned long total=0; unsigned long outp=0; FILE *of,*nf; int x; size_t count=1; if (argc < 4 || argc > 5) help_exit(1); skip=atol(argv[3]); if (argc == 5) total=atol(argv[3]); if ((of=fopen(argv[1],"rb"))==NULL) { printf("No %s file??\n",argv[0]); help_exit(1); } if ((nf=fopen(argv[2],"w"))==NULL) { printf("No %s file??\n",argv[0]); help_exit(1); } if(skip) fseek(of,skip,SEEK_SET); if (!total) { while (1) { long bread; bread=fread(buf,4096,1,of); printf(" %d ",bread); if( !bread) break; fwrite(buf,bread,1,nf); printf("."); } } else { while (outp < total) { long bsize; long bread; bsize=4096; if (total - outp < 4096) bsize=total-outp; bread=fread(buf,bsize,1,of); printf(" %d ",bread); if( !bread) break; fwrite(buf,bsize,1,nf); printf("."); outp += bread; } } fclose(of); fclose(nf); printf("\n"); exit(0); } help_exit(int arg) { printf("\n\nSKIPBYTE oldfile newfile skip-bytes [total-bytes]\n"); printf("Reads 'oldfile' skipping 'skip-bytes' bytes and stopping\n"); printf("after 'total-bytes' are copied (copied to newfile, not read\n"); printf("from oldfile\n"); printf("'total-bytes' is optional\n"); printf("\n\n(C)1993 Lawrence & Clark, Inc (781) 784-5537 (206) 323-2864\n\n\n"); exit(arg); }
Got something to add? Send me email.
More Articles by Tony Lawrence © 2011-05-08 Tony Lawrence
We're terrible animals. I think that the Earth's immune system is trying to get rid of us, as well it should. (Kurt Vonnegut)
Printer Friendly Version
dd for windows Copyright © January 1997 Tony Lawrence
Have you tried Searching this site?
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.
Contact us
Printer Friendly Version