Printf's format Specs 
Author Message
 Printf's format Specs


Quote:
> Hi. I want to output a table like data in stdout like this:
> LastName      FirstName    Age       Sex        Status
> string1       string2      string3   string4    string5
> how will i do it in printf's or fprintf's format specification in such a
> way that these strings will be alined?

You have to figure how wide you want each field to be. Then you can use a
numeric specifier in printf() format strings to place the strings. For
instance, if all the strings were to go in a 12 char field:

printf("%-12s%-12s%-12s%-12s%-12s\n", s1, s2, s3, s4, s5);

The minus sign means left justify. If you want right justification, leave it
out.



Sat, 25 Dec 1999 03:00:00 GMT  
 Printf's format Specs

Quote:

> Hi. I want to output a table like data in stdout like this:

> LastName      FirstName    Age       Sex        Status
> string1       string2      string3   string4    string5
>  ...           ...          ...       ...        ...

> how will i do it in printf's or fprintf's format specification in such a
> way that these strings will be alined? I'm having a hard time on this
> stuff already w/ trials and errors. I need help

> thanks.

> Kenneth

Basically, you do something like this :

for(i=0; i<NUM_ROWS; i++)
{
   for(j=0; j<NUM_COLS; j++)
      printf("%-15s", string[i][j]);
   printf("\n");

Quote:
}

Here NUM_ROWS and NUM_COLS are the dimensions of your string array,
assuming that's how your strings are stored. The important part here
is the "%-15s" format specifier; this pads the strings to 15 characters
and thus guarantees alignment (assuming that your strings aren't longer
than this!) 15 is an arbitrary number; use whatever field width you
find most appropriate.

HTH
--

"That first egg was named, 'Thought'."



Fri, 31 Dec 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. printf format specs

2. replacing printf with my printf, then calling C lib's printf

3. printf()'s full format

4. printf %E format doesn't work on PockePC

5. fscanf format specs

6. Are object and executable file format specs available?

7. COleCurrency::Format doesn't format!!!!

8. Certain fprintf() format specs won't deal with signs!

9. printf formats floats wrong?

10. printf() format extensions - looking for beta testers...

11. Printf in tabular format

12. Format string as a parameter to printf()?

 

 
Powered by phpBB® Forum Software