![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Banned
![]() ![]() |
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);
printf("This is a variable %3d. This is some text.\n", 15);That is all fine and dandy. Unsurprisingly, it aligns the variables and outputs: This is a variable 5. This is some text. This is a variable 15. 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. ... 15. 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. ... 15. This is some text. So. We could potentially...
This is all so silly, for such a seemingly simple aesthetic. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Nov 2007
Posts: 33
Rep Power: 0
![]() |
Re: printf Alignment
An indirect approach perhaps?
printf( "%-10s %3d This is some text\n", "Garbage", 5 );
printf( "%-10s %3d This is some text\n", "...", 15 );$ gcc foo.c $ ./a.exe Garbage 5 This is some text ... 15 This is some text
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
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? |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Dec 2007
Posts: 13
Rep Power: 0
![]() |
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:
C++ Syntax (Toggle Plain Text)
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. Last edited by bldnfx; Jan 22nd, 2008 at 7:25 PM. |
|
|
|
|
|
#5 |
|
Banned
![]() ![]() |
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.
|
|
|
|
|
|
#6 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#7 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4
![]() |
Re: printf Alignment
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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. |
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Nov 2007
Posts: 33
Rep Power: 0
![]() |
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Oct 2007
Posts: 39
Rep Power: 0
![]() |
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>
#include <string.h>
#define NEXTCOL 38
int main()
{
char *strs = "ABCDEF";
char *strl = "ABCDEFGHIJKL";
int sizesingle= 9;
int size[][2]= { 3, 3, -3, 3, 3, 9, -3, 9,
3, -3, 3, -9, -3, -3, -3, -9,
3, 15, 3,-15, -3, 15, -3,-15,
0, 0,
9, 3, -9, 3, -9, 9, -9, -3,
-9, -9, -9, 15, -9, -15, 9, 9,
9, -9, 9, -3, 9, 15, 9,-15,
0, 0,
15, 3, 15, 9, 15, -3, 15, -9,
15, 15, 15,-15, -15, 3, -15, 9,
-15, -9, -15, 15, -15,-15, -15, -3,
-1, 0
};
char buf[20];
int i = 0;
int j;
j = printf(" Short -- %d chars <%s> ", strlen(strs), strs);
while (j++ < NEXTCOL) putchar(' ');
printf(" Long -- %d chars <%s> \n", strlen(strl), strl);
printf("\n");
j = sprintf(buf,"%*s", sizesingle, strs);
j = printf(" %%%3ds (%2d) <%s> ", sizesingle, j, buf);
while (j++ < NEXTCOL) putchar(' ');
j = sprintf(buf,"%*s", sizesingle, strl);
j = printf(" %%%3ds (%2d) <%s> ", sizesingle, j, buf);
printf("\n");
j = sprintf(buf,"%*s", -sizesingle, strs);
j = printf(" %%%3ds (%2d) <%s> ", -sizesingle, j, buf);
while (j++ < NEXTCOL) putchar(' ');
j = sprintf(buf,"%*s", -sizesingle, strl);
j = printf(" %%%3ds (%2d) <%s> ", -sizesingle, j, buf);
printf("\n");
j = sprintf(buf,"%.*s", sizesingle, strs);
j = printf(" %%.%2ds (%2d) <%s> ", sizesingle, j, buf);
while (j++ < NEXTCOL) putchar(' ');
j = sprintf(buf,"%.*s", sizesingle, strl);
j = printf(" %%.%2ds (%2d) <%s> ", sizesingle, j, buf);
printf("\n");
j = sprintf(buf,"%.*s", -sizesingle, strs);
j = printf(" %%.%2ds (%2d) <%s> ", -sizesingle, j, buf);
while (j++ < NEXTCOL) putchar(' ');
j = sprintf(buf,"%.*s", -sizesingle, strl);
j = printf(" %%.%2ds (%2d) <%s> ", -sizesingle, j, buf);
printf("\n");
printf("\n");
while (size[i][0] != -1)
{
if (size[i][0] != 0)
{
j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strs);
j = printf(" %%%3d.%3ds (%2d) <%s> ", size[i][0], size[i][1], j, buf);
while (j++ < NEXTCOL) putchar(' ');
j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strl);
j = printf(" %%%3d.%3ds (%2d) <%s> ", size[i][0], size[i][1], j, buf);
}
printf("\n");
i++;
}
printf("done... \n");
return 0;
}
__________________
Testing 001 010 011 100.... |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| printf or cout? | d_heyzie | C++ | 15 | Apr 18th, 2006 4:57 AM |
| Printing escape sequences using printf()? | bivhitscar | C | 5 | Nov 3rd, 2005 11:08 PM |
| printing "%" with printf | ivan | C | 15 | Oct 20th, 2005 8:22 AM |
| How to say the size of an array using printf ? | colt | C | 2 | May 19th, 2005 4:25 PM |
| printf() | Planet_EN | C++ | 1 | May 3rd, 2005 5:04 PM |