C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-16-2005, 04:24 PM   #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.
Uncle Rico is offline   Reply With Quote
Old 08-16-2005, 04:40 PM   #2
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 4,990
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.*
Dave_Sinkula is offline   Reply With Quote
Old 08-16-2005, 05:10 PM   #3
Registered User
 
Join Date: Feb 2005
Posts: 11
Perfect! That's exactly what I needed, thanks
Uncle Rico is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22