![]() |
printf Alignment
A quick question. I know printf makes aligning variables easy. But what about absolute alignment? The only alignment I can figure out to do with printf is relative.
:
printf("This is a variable %3d. This is some text.\n", 5);That is all fine and dandy. Unsurprisingly, it aligns the variables and outputs: :
This is a variable 5. This is some text.But that is relative to the text that comes before it. What if we want absolute alignment, where the interval specifies the character's position relative to the start of the line? :
Garbage 5. This is some text.We can't use relative alignment, because then the 5 and 15 are no longer aligned with eachother, only with the text before it. Like so: :
Garbage 5. This is some text.So. We could potentially...
This is all so silly, for such a seemingly simple aesthetic. |
Re: printf Alignment
An indirect approach perhaps?
:
printf( "%-10s %3d This is some text\n", "Garbage", 5 );:
$ gcc foo.c |
Re: printf Alignment
You're simply saying that you want output where it outputs
blah blah blah 5. text text blah blah blah blah 15. text text So... in other words, you want the text to line up on each line in the same position regardless of the output before it, is that correct? |
Re: printf Alignment
Is there anything analogous to setw() available in C? I was able to make some nice columns regardless of the length of the members in the first position with:
:
And all columns come out nice nice. Of course, this is directly from a C++ program, not C. some_string is a word that varies that varies in length in each row. (It could be different sized numbers.) The words are usually no more than a dozen characters, and then "Number of Occurrences: " and the word count all line up neatly at column twenty, regardless of first string length. |
Re: printf Alignment
Salem, that would work, but unfortunately the text that comes beforehand is the result of multiple printf statements. So it's basically garbage text that I can't control. Unless I were to wrap it all into one string with sprintf. But yuck.
|
Re: printf Alignment
If Salem's suggestion won't work then I guess you could move the cursor to a specific location. How to do that is os-dependent. Here is another thread that discusses that a little bit.
|
Re: printf Alignment
Quote:
|
Re: printf Alignment
Quote:
Wrapping it into one string with sprintf() still requires you to keep track of the length of output since the last newline, but I suppose it's easier to count back if you do that. Rewinding the cursor to the first position on a line can often be done practically for console output, by outputting a '\r' (a carriage return with no line feed). The problem with that is: any text you write to the same line will typically overwrite whatever is on the line. It also does not work for other types of output device (eg files) .... the data is still in the file. It also relies on things like console drivers, screen drivers, etc .... depending on what operating system your program runs on, and what hardware (eg display device) is attached to the machine. |
Re: printf Alignment
Writing a wrapper for printf, using say vsnprintf() and some additional formatting of your own may seem a long way round, but it will give the most predictable results in the long run, and would be readily expandable to cater for future requirements.
Is glibc your standard C library by any chance? You can register your own printf formats in this library, though I don't know if they get passed enough information to do what you want to do. |
Re: printf Alignment
Quote:
printf() returns the number of characters output. So use the return value to count the number of characters already displayed and use the '*' formatting character to align your numbers.Another thing to know is how the %s modifier work. Run this to understand the details in how %n.ms formatting works::
#include <stdio.h> |
| All times are GMT -5. The time now is 3:52 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC