View previous topic :: View next topic |
Author |
Message |
haxan7
Joined: 27 Jul 2013 Posts: 79
|
sprintf/fprintf padding the string with spaces |
Posted: Sat Sep 07, 2013 5:37 am |
|
|
Compiler V4.140
I have tried fprintf(pc,"<%6s>", "a"); but it does not work.
Code: |
I want it to print "< a>". But it prints "<a>".
|
How do i fix this?
Is it even possible in CCS?
Last edited by haxan7 on Sat Sep 07, 2013 8:41 am; edited 2 times in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Sep 07, 2013 7:27 am |
|
|
Just put a space in the format string:
fprintf(pc,"< %5s>", "a");
Best Wishes |
|
|
haxan7
Joined: 27 Jul 2013 Posts: 79
|
|
Posted: Sat Sep 07, 2013 8:42 am |
|
|
Ttelmah wrote: | Just put a space in the format string:
fprintf(pc,"< %5s>", "a");
Best Wishes |
Code: | I want "< a>" Printed |
The forum removed my extra spaces. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Sep 07, 2013 12:15 pm |
|
|
If your string is only one character, then the same answer applies. Just put more spaces....
However it sounds as if what you want is to 'right justify'. If so, then you have to do this by filling.
So (for instance):
Code: |
#include <string.h>
//arrive here with your string in a variable 'a'
char filler[7]=" "; //note 6 spaces
fprintf(pc,"<%s%s>",filler+strlen(a),a);
|
This prints 6-the_length_of_'a' spaces from 'filler', then the string 'a'.
Beware - if 'a' is longer than 6 characters....
Best Wishes |
|
|
|