Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created July 27, 2016 19:58
Show Gist options
  • Save DreamVB/7fee31e5a46e5a46fd11dd5922cd30e6 to your computer and use it in GitHub Desktop.
Save DreamVB/7fee31e5a46e5a46fd11dd5922cd30e6 to your computer and use it in GitHub Desktop.
A small bit of code to convert TABS in a source file to spaces.
/*
FixTabs By DreamVB
Version 1.0
A small tool to convert \t tabs to a number of specified number of spaces.
Please feel free to to use as you like.
Please let me know if you found this code usfull e. [email protected]
*/
#include <stdio.h>
#include <string>
int main(int argc, char* argv[]){
FILE *fin = NULL;
FILE *fout = NULL;
int TabSize = 8;
int spc = 0;
char c = '\0';
if(argc < 3){
printf("Usage: <InFile> <OutFile> [TabSize]\n");
return 0;
}
if(argc == 4){
//Convert tabsize to int
TabSize = atoi(argv[3]);
}
//Check for vaild tabsize
if(TabSize < 0){
TabSize = 8;
}
//Open inputfile.
fin = fopen(argv[1],"r");
//Try and see if inputfile opened.
if(fin == NULL){
printf("IO/Error cannot open file\n");
return 0;
}
//Open output file
fout = fopen(argv[2],"w");
//See if output file opened.
if(!fout){
printf("IO/Error cannot write file\n");
return 0;
}
while(!feof(fin)){
//Read char
c = getc(fin);
if(!feof(fin)){
//Test for tab char
if(c == '\t'){
//Replace with 1 tab with a number of spaces.
do{
//Put space
fputc(' ',fout);
//INC space counter
spc++;
}while(spc % TabSize);
//Keep eveything going
continue;
}
//Put char into output file.
fputc(c,fout);
}
}
//Close files
fclose(fin);
fclose(fout);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment