APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

SKIPBYTE.C Tony Lawrence:

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.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Dos Programs - C code like dd but with only skip and count




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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





Anyone even peripherally involved with computers agrees that object-oriented programming (OOP) is the wave of the future. Maybe one in 50 of them has actually tried to use OOP – which has a lot to do with its popularity (Steve Steinberg)

If you understand something, it is probably already obsolete (James Burke)












This post tagged: