CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

sprintf/fprintf padding the string with spaces

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

sprintf/fprintf padding the string with spaces
PostPosted: Sat Sep 07, 2013 5:37 am     Reply with quote

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: 19568

View user's profile Send private message

PostPosted: Sat Sep 07, 2013 7:27 am     Reply with quote

Just put a space in the format string:

fprintf(pc,"< %5s>", "a");

Best Wishes
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Sat Sep 07, 2013 8:42 am     Reply with quote

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: 19568

View user's profile Send private message

PostPosted: Sat Sep 07, 2013 12:15 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group