TAB to SPACE conversion

I use gedit and although it has a setting to convert tabs to spaces, it doesn't work. At times I might have 4 levels of nesting and TABS of 8 just doesn't look good. So I cooked this little thing together that allows SourceFile, DestinationFile and number of spaces / TAB 1 - 32.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include    <unistd.h>
#include    <stdlib.h>
#include    <fcntl.h>
#include    <stdio.h>

    const char *Padd = "                                ";
    
// Application to convert tabs (0x9h) in .cpp files created by gedit to 4 spaces. 
int main ( int ArgC, char *ArgS[] ) {
    int InFile, OutFile;
    
    printf ("\n\t");
    
    if ( ArgC > 1) {
        if ( ArgC > 4 )
            printf ("Additional arguments will be ignored\n");
            
        if ( (InFile = open ( ArgS [1], O_RDONLY )) != -1 ) {
            char c;
            int TabSize = atoi ( ArgS [3]);
            
            printf ("Processing %s", ArgS [1]);
            OutFile = open ( ArgS[2] , O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR );
            
            while ( read ( InFile, &c, 1) ) {
                if ( c == 9 )
                    write ( OutFile, Padd, TabSize );
                else
                    write ( OutFile, &c, 1 );
            }
            
            close ( OutFile );
            close ( InFile );
        }
        else
            printf ("Failed to open %s", ArgS [1]);
    }
    else
        printf ("Insufficient parameters. Filename required");
        
    printf ("\n\n\t **** D O N E ****\n\n");
            
    return 0; 
}


I still prefer to do things without an IDE and most of my apps are console using ncurses, so this just makes cleaner looking code segments.
I did get it to work. Little did I realize it doesn't work well on existing files, but once spaces are set it does as expected on a new file. The unfortunate side affect is, therein after one can't use Shift-Tab or Tab to retabulate a selected block, so I'll be leaving it at the default setting.
Topic archived. No new replies allowed.