john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file tabs to 4 spaces

/* John Pfeiffer, 22Jun08 replace all tab characters (/t) with 4 spaces in an ascii text file */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
    char c;
    char outputfile[128]="notabs-";
    FILE* ifp;
    FILE* ofp;

    ifp = fopen(argv[1],"r");
    strcat(outputfile, argv[1]);
    ofp = fopen(outputfile,"w");

    if( (ifp == NULL) || (ofp == NULL))
    {    printf("\nERROR opening file, try tab-to-spaces.exe filename.txt\n");
        exit(1);
    }

    c = getc(ifp);
    while( c!= EOF)
    {
        if( c == '\t')
        {   putc(' ',ofp); 
            putc(' ',ofp); 
            putc(' ',ofp); 
            putc(' ',ofp); 
        }
        else
        {   putc(c,ofp);
        }
        c = getc(ifp);
    }

    fclose(ifp);
    return 0;
}

  • « dirlist
  • httpget cnet »

Published

Jun 6, 2008

Category

c

~76 words

Tags

  • 4 10
  • c 95
  • file 92
  • spaces 1
  • tabs 3
  • to 63