Thread: Need help calculating number of spaces with tabs

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Need help calculating number of spaces with tabs

    I am trying to figure out how to accurately line up some data in two separate columns in an output file that is basically a rewritten version of an input file. I know I could separate all non-whitespace-characters in one column from all non-whitespace-characters in another column (in theory), and then just separate the separate strings in the output with a specific constant number of spaces, but since I've already written code that attempts to use a different approach (i.e. basically reformat a line read from the input file to have the correct number of spaces between the two columns in the output file), I would like to get it working using my current approach, if possible.

    Basically, the problem I'm experiencing is that the input file that I'm attempting to rewrite a different way doesn't just have space characters separating the two columns in the lines of the file. Instead, it also has tab characters. I know the '\t' character is an unspecified number of spaces, and different applications use a different length, so basically, what I need to do is figure out how many spaces it SHOULD represent (in the editor that I'm viewing the file in, gedit), and then simply count the number of tabs found in each line between the two columns, and then replace the tabs with the correct number of spaces, so that all data lines up.

    Any ideas?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since each tab is equal to exactly X number of spaces, replace each tab with X spaces. You pick X. If you want to know how many spaces your text editor uses for tabs, check the documentation.

    Jim

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