PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > simple printf question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
simple printf question

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 18h37   #1
Bint
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut simple printf question

Hi,

I have a giant string buffer, and I want to print out small chunks of it at
a time. How do I print out, say 20 characters of a string?

Is it like this?

printf("%20s",mystring);

I can change the start point of the string, I just don't know how to tell it
to only print out X number of characters from it.
Thanks
B


  Réponse avec citation
Vieux 17/10/2007, 18h41   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple printf question

Bint said:

> Hi,
>
> I have a giant string buffer, and I want to print out small chunks of it
> at
> a time. How do I print out, say 20 characters of a string?
>
> Is it like this?
>
> printf("%20s",mystring);
>
> I can change the start point of the string, I just don't know how to tell
> it to only print out X number of characters from it.


printf("%.20s", mystring);

Note the dot in the format specifier.

Covered in K&R2 p244.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 18/10/2007, 01h41   #3
Tor Rustad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple printf question

Bint wrote:
> Hi,
>
> I have a giant string buffer, and I want to print out small chunks of it at
> a time. How do I print out, say 20 characters of a string?
>
> Is it like this?
>
> printf("%20s",mystring);


printf("%.*s", len, mystring);


--
Tor <torust [at] online [dot] no>

C-FAQ: http://c-faq.com/
  Réponse avec citation
Vieux 18/10/2007, 04h34   #4
husterk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple printf question

On Oct 17, 1:37 pm, "Bint" <b...@csgs.com> wrote:
> Hi,
>
> I have a giant string buffer, and I want to print out small chunks of it at
> a time. How do I print out, say 20 characters of a string?
>
> Is it like this?
>
> printf("%20s",mystring);
>
> I can change the start point of the string, I just don't know how to tell it
> to only print out X number of characters from it.
> Thanks
> B


Bint,

Another more flexible method would be to snprintf() your larger string
into a temporary string buffer and then output the temporary buffer
using a standard unformatted printf(). This will allow you to
dynamically change the size of your output string by supplying a
variable length for your temporary string buffer in the snprintf()
routine (which cannot be accomplished using the "%.20s" method
described by Richard.

Keith
http://www.doubleblackdesign.com

  Réponse avec citation
Vieux 18/10/2007, 06h54   #5
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple printf question

husterk said:

<snip>

> Another more flexible method would be to snprintf() your larger string
> into a temporary string buffer and then output the temporary buffer
> using a standard unformatted printf(). This will allow you to
> dynamically change the size of your output string by supplying a
> variable length for your temporary string buffer in the snprintf()
> routine (which cannot be accomplished using the "%.20s" method
> described by Richard.


There is really no need to go to all that trouble.

printf("%.*s\n", nchars, mystring);

Again, this is documented very clearly in K&R2.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 20/10/2007, 15h57   #6
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple printf question

"Richard Heathfield" <rjh@see.sig.invalid> a écrit dans le message de news:
CaOdnVqVmdolaYvaRVnyiwA@bt.com...
> husterk said:
>
> <snip>
>
>> Another more flexible method would be to snprintf() your larger string
>> into a temporary string buffer and then output the temporary buffer
>> using a standard unformatted printf(). This will allow you to
>> dynamically change the size of your output string by supplying a
>> variable length for your temporary string buffer in the snprintf()
>> routine (which cannot be accomplished using the "%.20s" method
>> described by Richard.

>
> There is really no need to go to all that trouble.
>
> printf("%.*s\n", nchars, mystring);
>
> Again, this is documented very clearly in K&R2.


Be careful to cast nchar as (int) if it has a different type, such as
size_t.

--
Chqrlie.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14877 seconds with 14 queries