PADDER.C Tony Lawrence:

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," 7Warning: 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.





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

Printer Friendly Version

->
-> Dos Programs: C code to pad text files to fixed length records.




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





[C has] the power of assembly language and the convenience of … assembly language. (Dennis Ritchie)

On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?"...I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. (Charles Babbage)








This post tagged: