Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
I still can't line up vertical columns, code below, as one person suggested I use the space bar instead of tabs, I did that. I erased my code that looked just like this, but had used tabs to do it., then I rewrote it and aligned Unit and price, and Purchase and Date with the space bar. But the output is exactly the same as before. Is this really just trial and error trying to make a vertical column? there is no way to do it, like with placeholders (left align, right align, etc?? ) Since the output is aligning about 4 or 5 spaces to the left, do I just need to move it in my code 4 or 5 spaces to the right? So in the code it won't look right, but the program will? No way to make them both the same?

Code:
printf ("Item:		Unit		Purchase\n" );
	 printf ("                    Price          Date\n" ) ;

output for running program...

Item: Unit Purchase
Price Date

I just did it, but I had to keep playing with the spaces until the output looked right, is this the right way to program? This is what I had to leave it at to get the proper output..... crazy! okay, in the edit screen on here and while posting, my code looks as I intended. But I just looked at my post after posting it, and the code tags ruin the integrity of what I just posted, moving everything around. what a pain!! So below I will try again, the first one without code tags, and the 2nd with. Okay, now I see they both look the same on the finished post, with our without code tags. So I guess I cannot post my code the way I wrote it, there is no way to show you the spacing and alignment that I used. I can just say that it was a very unscientific approach, just trial and error on placement, until the output was correct below. Oh, I also see now that even my copy/paste from terminal doesn't look right!!!!!!! I can assure you that when I wrote it, it was aligned properly, and when I hit the post button it was perfect, but between posting and reading the post, MR somehow changes it and it doesn't stay as I wrote it!!! What a pain in the tooth!!!!


Code:
printf ("Item:		Unit		Purchase\n" );
	printf ("                Price           Date\n" ) ;

printf ("Item:		Unit		Purchase\n" );
	  printf ("                           Price             Date\n" ) ;


program output from terminal

Code:
Enter purchase date (mm/dd/yy): 11/09/10
Item:		Unit		Purchase
                Price          Date
 
Last edited by a moderator:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
Please fix your CODE tags, you are missing a "[" in the closing tag so it doesn't format right. As posted, I would not guess your code would align properly.

If all you have is C and printf/scanf then yes, this is how it is done. "Real" formatting is better done using third party tools and libraries.

e.g. if your goal is formatted printed output you could output TeX or PostScript as a text file and process/print that. If you want it on the screen use Cocoa.

EDIT: Are you using the "#" pound/hash button in the edit window or entering the CODE tags yourself? The easiest way to enter code is to hit the "#" then paste between the tags. The program output also needs to be within the tags if you want any hope of aligning things.

EDIT 2: Alignment of strings isn't hard, just line up the opening double quotes and use spaces where necessary.

B
 
Last edited:

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
Hmmm...I see that missing bracket you are talking about. Strange both my posts are missing only that, and yes I just copy and pasted between the two CODE words, didn't type them myself. I don't know why it disappeared. But thanks for pointing that out as I didn't notice it, I can make sure next time it's correct.

Ahhh, I just found a better way to align the columns than have two printf strings and manually play with spacing.. it's the following code.....


Code:
printf ("Item\tUnit\tPurchase\n\tPrice\tDate\n") ;

it came out perfect in the terminal, much easier!!
 
Last edited by a moderator:

McGordon

macrumors member
Dec 28, 2010
63
1
Scotland
One more thing to check is that you're using fixed width fonts in both the editor where you type your code and the terminal window where your code is running.

With variable width fonts, the calculations get a lot more complicated to line things up.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Ahhh, I just found a better way to align the columns than have two printf strings and manually play with spacing.. it's the following code.....


Code:
printf ("Item\tUnit\tPurchase\n\tPrice\tDate\n") ;

it came out perfect in the terminal, much easier!!

Beware an item you're printing being too long to fit in one tabstop.

-Lee
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
Code:
printf ("Item\tUnit\tPurchase\n\tPrice\tDate\n") ;

Thanks for finally getting the CODE tags right.

Better is relative. You still have no guarantees how the tab stops are set up if you switch anything. If I have tab stops set to 4 characters instead of 8 your code will no longer line up because Price is longer than 4 characters.

e.g. http://www.mackb.com/Uwe/Forum.aspx/mac/4700/Q-Tab-spacing-in-Terminal-app

EDIT:
If you're going to use tabs and distribute your code you need to make sure you control the tab stops. See attachment for example.


B
 

Attachments

  • Screen shot 2011-01-04 at 11.11.12 AM.png
    Screen shot 2011-01-04 at 11.11.12 AM.png
    31.2 KB · Views: 90
Last edited:

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
I just checked my editor, (textwrangler) and I'm using fixed width fonts (monaco) and my tab stops are set at 4.

However I don't know how to choose fixed width fonts in my terminal. How?
 

McGordon

macrumors member
Dec 28, 2010
63
1
Scotland
I just checked my editor, (textwrangler) and I'm using fixed width fonts (monaco) and my tab stops are set at 4.

However I don't know how to choose fixed width fonts in my terminal. How?

If you don't know then that's good as it means you haven't changed it and it'll be fixed width by default.

Terminal Preferences -> Settings -> Text tab -> font
That's in the one that comes with Snow Leopard, it used to be different.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
my tab stops are set at 4.

That explains why your original experiment with literal tabs (^I not \t) didn't work. Terminal defaults to tab stops of 8.

NOTE: As most code editors do, TextWrangler has an option to auto expand tabs into spaces. I'd suggest you use it.

B
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Don't use tabs at all, use the field width in printf.

Code:
printf("%-10s%-10s%-10s\n%-10s%-10s\n", "Item", "Unit", "Purchase", "Price", "Date");

Note the - in front of the width, it makes the output left justified, which is what you want in this case.
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
You guys are awesome! A lot of great suggestions, good explanations, I understand a lot more now about how to do it. Now I know also why my literal tabs were not coming out the same, because terminal and my editor are different, so they were displayed by terminal the way it does tabs. I get it. I like the suggestion of the field width string, I was wondering how to do that with printf words, rather than values like %d, or %f.

Thanks! I finished my exercises, now on to Chapter 4.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
I was wondering how to do that with printf words, rather than values like %d, or %f.

I don't think you can, not that I'm aware of at least. But since this is for a table, that format will make more sense when you print the table (in some kind of loop).

If you wanted to you could create a pre formatted string just for the header as well.

Code:
const char *tableheader = "Item      Unit      Purchase\n          Price     Date\n";
puts(tableheader);
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Don't use tabs at all, use the field width in printf.

Code:
printf("%-10s%-10s%-10s\n%-10s%-10s\n", "Item", "Unit", "Purchase", "Price", "Date");

Note the - in front of the width, it makes the output left justified, which is what you want in this case.

As I was reading down I was thinking exactly the same thing. There are A LOT of ways printf can help formatting.

Another example is using an integer so you can calculate how wide things need to be and supply the width at run time.

Code:
	int size = 10;
	printf("%-*s%-*s%-*s\n%-*s%-*s\n", size, "Item", size, "Unit", size, "Purchase", size, "Price", size, "Date");
	printf("\n\n");
	size = 20;
	printf("%-*s%-*s%-*s\n%-*s%-*s\n", size, "Item", size, "Unit", size, "Purchase", size, "Price", size, "Date");
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
There are A LOT of ways printf can help formatting.
Definitely lots of ways to skin these cats.

It's also worth remembering that in real life, in our GUI age, you probably would not be using printf to display this to a terminal/fixed width display. You'd be far more likely to do something different like use the widgets and controls provided by the UI of your OS or a library or, if you are really wanting flexible tabular output, drive your favorite spreadsheet program to do it for you.

B
 

chown33

Moderator
Staff member
Aug 9, 2009
10,748
8,421
A sea of green
It's also worth remembering that in real life, in our GUI age, you probably would not be using printf to display this to a terminal/fixed width display. You'd be far more likely to do something different like use the widgets and controls provided by the UI of your OS or a library or, if you are really wanting flexible tabular output, drive your favorite spreadsheet program to do it for you.

There's also the ncurses library. Command-line tools like 'top', 'pico', and 'vi' use it.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
Most real life command line tools does this.

Yes, and no.

And I'm not just referring to using libraries like ncurses where formatting/ interactive control of the screen is important.

e.g. many of Apple's own command line tools will output a plist instead of screen formatted text for ease of parsing by GUI tools. For example:
Code:
diskutil list -plist

I've written plenty of command line code that will output to the screen in a CSV format, intended to be interpreted by another program like a spreadsheet. Other tools may output HTML or XML to be interpreted by a browser.

B
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Yes, and no.

And I'm not just referring to using libraries like ncurses where formatting/ interactive control of the screen is important.

e.g. many of Apple's own command line tools will output a plist instead of screen formatted text for ease of parsing by GUI tools. For example:
Code:
diskutil list -plist

I've written plenty of command line code that will output to the screen in a CSV format, intended to be interpreted by another program like a spreadsheet. Other tools may output HTML or XML to be interpreted by a browser.

B

I said 'most' not all, you get a plist output since you specify the plist flag not surprisingly.
Code:
diskutil info /dev/disk0
Since the thread is about printf and c specifically it's not out of place to talk about printf formatting, it IS still used in these circumstances.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
I said 'most' not all, you get a plist output since you specify the plist flag not surprisingly.
Code:
diskutil info /dev/disk0
Since the thread is about printf and c specifically it's not out of place to talk about printf formatting, it IS still used in these circumstances.

I'm sure that at some level printf is used even to generate the plist, but it is accessed by the code as part of a higher level Objective-C function.

My point is that cybrscot ultimately wants develop in Cocoa, and there many of the screen formatting aspects of printf (vestiges of punchcards and teletypes) that we are focusing on in this thread will go away and be replaced by some completely other (hopefully simpler and higher level) interface.

As I mentioned earlier in the thread, if you're trying to build a report (invoice, receipt, purchase order, etc..) of the kind that cybrscot is working on that is intended for printing or even viewing on the screen today you would likely use some other way to format it (TeX, PostScript, PDF, HTML, XML, RTF, ...) that would give you more options and flexibility.

B
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
I'm sure that at some level printf is used even to generate the plist, but it is accessed by the code as part of a higher level Objective-C function.

Probably, but I was referring more to the column formatted output to stdout.

My point is that cybrscot ultimately wants develop in Cocoa, and there many of the screen formatting aspects of printf (vestiges of punchcards and teletypes) that we are focusing on in this thread will go away and be replaced by some completely other (hopefully simpler and higher level) interface.

As I mentioned earlier in the thread, if you're trying to build a report (invoice, receipt, purchase order, etc..) of the kind that cybrscot is working on that is intended for printing or even viewing on the screen today you would likely use some other way to format it (TeX, PostScript, PDF, HTML, XML, RTF, ...) that would give you more options and flexibility.

B

I see your point, and I agree. Except the part that it will go away, simply use the tool appropriate for what's the given task. For end user desktop GUI applications printf is completely irrelevant.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
simply use the tool appropriate for what's the given task. For end user desktop GUI application printf is completely irrelevant.
My point exactly. For cybrscots long term goal this is the wrong place to be concentrating tons of effort.

It's like learning how to use an abacus, certainly useful but how often will you use it over the alternatives?

B
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
My point exactly. For cybrscots long term goal this is the wrong place to be concentrating tons of effort.

It's like learning how to use an abacus, certainly useful but how often will you use it over the alternatives?

B

If anything this teaches how to use documentation and your imagination to solve a problem. I think you do that continuously as a programmer.

EDIT: oh and add some commas to my above example and you get something that can be parsed as easily by a computer as by your own eyes.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.