LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Proper whitespace formatting on stdout (https://www.linuxquestions.org/questions/programming-9/proper-whitespace-formatting-on-stdout-346963/)

R00ts 07-26-2005 11:26 AM

Proper whitespace formatting on stdout
 
I'm hoping that programmers here can share their experiences in writing code that properly aligns columns of data with whitespace on print out. This is a generic programming question that doesn't apply to any specific language, but right now I'm working on a C++ program where I would like data to print out properly. For example, here's a couple output excerpts:


>>> RUN # 27 of 28
> k = 10
> resizing counts and centers
> randomizing centers: 4937 2714 504 4306 1000 6105 6023 267 7988 3903
> clustering data
> finding best points
[#]: points, weights, distances
[0]: 4919, 0.11931, 0.000231612
[1]: 5858, 0.108499, 0.00142816
[2]: 4552, 0.0542497, 0.000267208
[3]: 5258, 0.175146, 0.00141083
[4]: 5865, 0.0286621, 0.00126036
[5]: 5044, 0.0814242, 0.00111759
[6]: 8310, 0.0700188, 0.00709409
[7]: 6252, 0.22771, 0.00141831
[8]: 619, 0.0617872, 0.00862163
[9]: 3684, 0.0731925, 0.00237783
> saving results



---------------------------------------------------------------
>>>>> RUN # 24
> Number of clusters: 9
[cluster #]: point, weight, distance
[0]: 4919, 0.0617872, 0.000231612
[1]: 5858, 0.0875731, 0.00142816
[2]: 4552, 0.00694238, 0.000267208
[3]: 5258, 0.136765, 0.00141083
[4]: 5865, 0.0664485, 0.00126036
[5]: 5284, 0.176931, 0.00298629
[6]: 8235, 0.0463156, 0.00751788
[7]: 5044, 0.200436, 0.00111759
[8]: 619, 0.216801, 0.00862163
> Average field error: 416.391(53.9588%)
> Sum error: 7078.64(29.2063%) actual: 24236.7
> Field errors:
[0]: 3740.53(27.2963%) actual: 13703.4
[1]: 603.642(27.7839%) actual: 2172.63
[2]: 605.385(26.4766%) actual: 2286.49
[3]: 0.08088(100%) actual: 0.08088
[4]: 2.1151(35.3533%) actual: 5.98277
[5]: -397.187(-86.4649%) actual: 459.362
[6]: 19.0059(53.041%) actual: 35.8325
[7]: -1102.79(-23.7254%) actual: 4648.16
[8]: 0.214975(55.1708%) actual: 0.389654
[9]: 294.663(57.0844%) actual: 516.188
[10]: 196.59(84.9495%) actual: 231.42
[11]: 33.7981(38.6778%) actual: 87.3837
[12]: 70.5228(94.4974%) actual: 74.6294
[13]: 0(0%) actual: 0
[14]: 0.139298(49.5039%) actual: 0.281389
[15]: 1.95268(71.5344%) actual: 2.72971
[16]: 10.0161(85.7391%) actual: 11.682

> Clustering error: 7519.72(8.17334%)
> Average clustering error: 835.524(34.5529%)
> Cluster errors:
[0]: 375.21(27.5788%)
[1]: 1883.02(71.0293%)
[2]: 236.501(72.7497%)
[3]: 2484.86(61.6242%)
[4]: 348.939(25.9014%)
[5]: 1706.57(40.3851%)
[6]: 35.5407(2.79514%)
[7]: 32.0395(0.829181%)
[8]: 417.035(8.08319%)



(I use tabs to try to align most of the fields here). I suppose I could do something like printf to dictate how many digits/decimal places to print the data too, but I can't know how large/small the numbers are until I actually use my program to compute the results.


So is there some golden library or function where I can get the data to align nicely, or figure out exactly how many characters that the program intends to print out before it actually prints them? It would be helpful if this was a C++ construct, because I don't really want to change all my C++ output file construction code to use C constructs and file pointers. Thanks ;)

Hko 07-26-2005 11:56 AM

Re: Proper whitespace formatting on stdout
 
Quote:

Originally posted by R00ts
I'm hoping that programmers here can share their experiences in writing code that properly aligns columns of data with whitespace on print out. This is a generic programming question that doesn't apply to any specific language, but right now I'm working on a C++ program where I would like data to print out properly. For example, here's a couple output excerpts:

[..snip..]

I suppose I could do something like printf to dictate how many digits/decimal places to print the data too, but I can't know how large/small the numbers are until I actually use my program to compute the results.
Sure, you can specify a field width and a precision for each number with printf(). See "man 3 printf" and look for "field width" and "precision". You could even make the field width and precision dynamic, by generating the format string for printf() with snprintf() if you realy need to.

BTW Python has its own nice output formatting features.

schneidz 07-27-2005 03:46 PM

is there any program that can do this in a script?

thanks,

schneidz

R00ts 07-27-2005 05:21 PM

Quote:

Originally posted by schneidz
is there any program that can do this in a script?

thanks,

schneidz


What you said doesn't make sense. Did you mean does anyone have any code for performing this in a script? And what scripting language are you talking about? HKo already kindly posted that link to Python output formatting. :)

schneidz 07-28-2005 11:11 AM

that was phrased kinda' funny (my apologies).

what i meant was can i call a c-program/s in a bash script that will do this. (i.e.- will a mixture of sed-awk columnize my output)

\t tab-stops sometimes look funny, and pr adds date stamped/ paginated headers with mad spaces (i couldn't figure out from the man how to suppress that).

eddiebaby1023 07-30-2005 04:27 PM

You can use printf in a bash script.


All times are GMT -5. The time now is 10:19 AM.