|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"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. |
|
![]() |
| Outils de la discussion | |
|
|