Thread: Replace tabs with spaces

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    2

    Replace tabs with spaces

    Hey,

    i want to replace tabs with spaces but i didn't get it.
    I also tried to count all the chars that i read from a file but that does also not work.

    Here's what i have so far (fP is just a file)

    Code:
    void ReplaceTab(FILE *fP)
    {
        int char_ = 0;
       
        if (fP != NULL)
        {
            while((char_ = fgetc(fP)) != EOF)
            {
                if(char_ == '\\') 
                {
                    char_ = fgetc(fP);
                   
                    if(char_ == 't') // 
                    {
                        // Here's where i stuck. I just want to do it with c, no c++
                        printf("\t");
                    }
                    else
                    {
                        putchar(92); // == \
                        putchar(char_);
                    }
                }
                else
                {
                    putchar(char_); // To print the chars
                }
            }
        }
        else
            printf("The file does not exists!");
            exit(1);
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are not replacing tabs with spaces. You are replacing "\\t" with "\t". In other words, you are replacing a backslash followed by the letter 't' with a tab. Also (if you're unaware), you are actually just printing the replacements to the console. The file is not modified at all.

    If you wanted to replace tabs with spaces, you would need something like this:
    Code:
    if (char_ == '\t') putchar(' ');
    Of course the number of spaces per tab would depend on what you want your tab width to be.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    2
    I know but i just wrote it to let you know where i stuck.
    The normal tab width is 8 as far as i know and i want to keep it like that.

    I have some \t in my file and i want to replace them, that's what i WANT to do.
    But yeah ... how can i convert the tab to as many spaces as needed? Sometimes there are just 2 spaces needed and sometimes there are 6.?
    Last edited by Charted; 12-20-2014 at 05:34 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    But yeah ... how can i convert the tab to as many spaces as needed? Sometimes there are just 2 spaces needed and sometimes there are 6.?
    This is not a trivial problem for a beginner. You need to keep track of how many characters you are printing and only add spaces up to your tabstop column.

    Here is an example of what I'm talking about:
    Code:
    const int tabwidth = 8;
    int column = 0;
    ...
    while(...)
    {
        if (c == '\t')
        {
            int spaces_needed = tabwidth - column % tabwidth;
            column += spaces_needed;
        }
    I will leave it as an exercise for you to figure out when to increment the column value and how to handle newline characters. Also, if you want this to work with a non ASCII encoding (like UTF-8), then good luck
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    That's detab from K&R Chapter 1.
    This is what I came up with:

    Code:
    #include <stdio.h>
    #define TABS 4
    #define MAXLINE 100
    
    
    
    
    int main(int argc, const char * argv[])
    {
    
    
        char newline[MAXLINE], c;
        int i;
        int cnt = 0;
        
        printf("Give me a line sucker!\n");
        
        while ((c = getchar()) != '\n') {
            if (c == '\t') {
                do{
                    newline[cnt] = ' ';
                    ++cnt;
                }while (cnt % TABS);  //find the Tab spot by modulus. Clean                                       
                                       //division (no remainder) defines a Tab spot.
            
            }
            else {
                newline[cnt] = c;
                ++cnt;
            }
        }
        
        for (i = 0; i < cnt; i++) {
            printf("%c",newline[i]);
        }
        printf("\n");
        
        
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by bithub View Post
    This is not a trivial problem for a beginner. You need to keep track of how many characters you are printing and only add spaces up to your tabstop column.
    I agree, it's not for rank beginners, but it's not exactly an advanced problem either except in some special cases.

    When outputting a character, all that is necessary is to track the displacement from the beginning of the line. When a tab is encountered, work out the separation between the next character and the next tab stop, and output that number of spaces.


    Where it can be entertaining is that tab stops themselves are variable - some editors specify different intervals 2,3,4,5,6,8 etc, some editors allow the user to specify all tab stops, but information about the tab stops is not typically written to the actual file (instead being stored in some configuration file for the editor itself). That means the set of tab stops needs to be a program input (e.g. from a config file, command line options, and some sensible defaults if no such input is supplied).

    It would be necessary to specify the behaviour required for any character input/encoding. What should happen if unprintable characters are encountered? How about handling vertical tabs? For a somewhat advanced topic, what about localisation (e.g. handling files for non-english speakers)?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. replacing tabs with spaces
    By marto1914 in forum C++ Programming
    Replies: 0
    Last Post: 10-03-2010, 12:44 AM
  2. How do you indent: spaces or tabs?
    By EVOEx in forum General Discussions
    Replies: 51
    Last Post: 09-24-2009, 07:10 AM
  3. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  4. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  5. tabs to spaces with dev c++ v.4
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 02:07 PM