Thread: Formatting output into even columns?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Formatting output into even columns?

    Hey guys, quick question. Is there anything in C (working in Linux here) that allows you to format output into even looking columns even if the corresponding output is printed horizontally? ..err, that's hard to explain. Basically I've got some code that performs DNS lookups on a list of machines (functionality is irrelevant to my question) and outputs the results. Right now, the output might look something like this:

    Code:
    host.domain.com              12.12.12.12
    host123.domain.com              123.123.123.123
    h.domain.com              21.21.21.21
    blah.domain2.com              32.32.32.32
    aaa.domain.com              111.111.111.111

    while it displays the right information, the output of the program as you can see is uneven and looks generally sloppy. What I'd like to do is have it's output look more like:

    Code:
    host.domain.com                 12.12.12.12
    host123.domain.com           123.123.123.123
    h.domain.com                      21.21.21.21
    blah.domain2.com               32.32.32.32
    aaa.domain.com                  111.111.111.111
    so that even though the data that goes together (host -- ip) is printed horizontally, columns of the text are outputted evenly. Is this possible ? Thanks.

    --EDIT--:
    In the example of how I want the output to look it still came out uneven, I guess thanks to how the forum parses my thread. Trust me though, the first number in the first octect of each IP in my example all line up >_<
    Last edited by Uncle Rico; 08-16-2005 at 04:31 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    There are width specifiers for printf.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const struct
       {
          const char *host, *ip;
       } text[] =
       {
          {"host.domain.com", "12.12.12.12"},
          {"host123.domain.com", "123.123.123.123"},
          {"h.domain.com", "21.21.21.21"},
          {"blah.domain2.com", "32.32.32.32"},
          {"aaa.domain.com", "111.111.111.111"},
       };
       size_t i;
       for ( i = 0; i < sizeof text / sizeof *text; ++i )
       {
          printf("%-64s %s\n", text[i].host, text[i].ip);
       }
       return 0;
    }
    
    /* my output
    host.domain.com                                                  12.12.12.12
    host123.domain.com                                               123.123.123.123
    h.domain.com                                                     21.21.21.21
    blah.domain2.com                                                 32.32.32.32
    aaa.domain.com                                                   111.111.111.111
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Perfect! That's exactly what I needed, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. formatting output
    By kisiellll in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 03:53 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Output Columns form a file
    By Cdrwolfe in forum Tech Board
    Replies: 2
    Last Post: 03-22-2006, 05:12 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM