A simple C program that pads text files to fixed length records. Was at one time very useful; is probably not today, but who knows?
I wrote this because I needed to bring some files into Filepro as fixed length fields.
It was written on Microsoft DOS, but will compile on Linux (though not on OS X - missing "fcloseall").
Usage: PADDER INFILE OUTFILE PAD-SIZE
#include <stdio.h> #include <string.h> char big_buff[1024]; main(argc,argv) int argc; char **argv; { char *bb=big_buff; FILE *ipf,*opf; int width=100; int warning=0; int maxwidth=0; int line=0; if (argc < 3) help(argv[0]); if (argc == 4) width=atoi(argv[3]); if (width < 1) width=100; if ((ipf=fopen(argv[1],"r"))==NULL) { fprintf(stderr,"Can't open %s for input\n",argv[1]); help(argv[0]); } if ((opf=fopen(argv[2],"w"))==NULL) { fprintf(stderr,"Can't open %s for output\n",argv[1]); help(argv[0]); } while (fgets(bb,1024,ipf)) { char *tp; int mwidth; line++; if (tp=strchr(bb,'\n')) *tp=0; if ((mwidth=strlen(bb)) > width ) { if (++warning == 1) fprintf(stderr,"\007Warning: Input width exceeds %d\n",width); fprintf(stderr,"Input line %5d , truncated %3d chars:\n %s\n",line,mwidth-width,bb+width); if (mwidth > maxwidth) maxwidth=mwidth; } fprintf(opf,"%-*.*s\n",width,width,bb); } fcloseall(); if (maxwidth) fprintf(stderr,"(Max input width was %d)\n",maxwidth); exit(0); } help(name) char *name; { fprintf(stderr,"Usage:\n%s input-file output-file [pad-size]\n",name); fprintf(stderr,"[pad-size] defaults to 100\n"); exit(1); }
Got something to add? Send me email.
More Articles by Tony Lawrence © 2011-04-25 Tony Lawrence
Simplicity is prerequisite for reliability. ((Edsger W. Dijkstra)
Printer Friendly Version
Pads text files to fixed length records 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