formatting the output using c
i have multiple outputs that i need to print as columns side by side. is there any way to achieve 开发者_开发技巧this using C? something like the output of ls -a i.e. i want to print the first column data, then the second and so on
You can do this with format strings.
See the printf(3) manpage for more info.
You'll want to specify field widths, this question has a pretty good example of what you're looking for.
EDIT (changing my whole answer):
For the formats that you get for free with printf (including some to do with length): http://www.cplusplus.com/reference/clibrary/cstdio/printf/
I would avoid using tabs to do this. Tabs can be interpretted in different ways on different machines (2 spaces, 4 spaces, n spaces). If this is a fun app just for you on your machine, tab away...but the following example is just as easy.
RIGHT JUSTIFY (8 is the width, s means string, | is just to show spacing):
printf("|%8s|%8s|","try","this");
would result in this:
| try| this|
LEFT JUSTIFY (- means left-justify, 8 is the width, s means string, > is just to show spacing):
printf("%-8s%-8s>","try","this");
would result in this:
try this >
If you mean automatically calculating the number of columns and their widths and so on, you can't do this with printf()
alone. However, it's possible to add extra code to precompute the number and width of columns. For instance, consider the OpenBSD code for ls
.
Let me make sure I understand; you have some code like
while (some_condition)
{
bread();
bwrite();
pgsize();
}
and you want to display the outputs of those functions as
bread bwrite pgsize
1234 5678 1024
2345 6789 1024
Is that close to what you meant?
If so, then you have a couple of choices. First, each function prints its output, but without a newline:
void bread()
{
...
printf("%*.*d", field_width, precision, value);
fflush(stdout)
...
}
where field_width
is the minimum width of the column, precision
indicates the minimum number of digits to be printed, and value
is whatever your function is printing. The *
in the conversion specifier allows you to pass the width and precision as arguments to printf(), i.e.,
printf("%*.*f", 10, 2, 3.14159);
is the same as
printf("%10.2f", 3.14159);
The chief advantage of using the *
flags in the conversion specifier means you can adjust your output format at runtime. Note that *
in a conversion specifier means something completely different in scanf().
Either the last function called or the caller will need to write a newline to the output stream.
Alternately, you can have each function return its value to the caller, and let the caller do all the output:
while (some_condition)
{
int r = bread();
int w = bwrite();
int s = pgsize();
printf("%*.*d%*.*d%*.*d\n", rwidth, rprec, r, wwidth, wprec, w, swidth, sprec, s);
}
where rwidth, rprec, etc., have all been declared and set elsewhere.
using "\t"
and "\n"
Example:
for(int i = 0 ; i < m ; ++i)
{
for(int j = 0; j < n ; ++j
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
精彩评论