Truncate a string using printf

A

Alessio

Hi,


there's a way to truncate a string using printf ?
For example, I've a string that is 50 chars long and I want to output to
video only first 20 chars.
How ?

Alessio.
 
N

Noob

Alessio said:
there's a way to truncate a string using printf ?
For example, I've a string that is 50 chars long and I want to output to
video only first 20 chars.
How ?

Have you tried the following?
buf[21] = 0;
 
B

bartc

Alessio said:
Hi,


there's a way to truncate a string using printf ?
For example, I've a string that is 50 chars long and I want to output to
video only first 20 chars.
How ?

Try a format string such as "%.20s"
 
B

Ben Bacarisse

Alessio said:
there's a way to truncate a string using printf ?
For example, I've a string that is 50 chars long and I want to output
to video only first 20 chars.
How ?

Look at whatever documentation you have for printf. See what you can
have in a %s format specifier other than just the % and the s. One of
these things will give you the answer!

I am being cryptic, in part, because this is the season of coursework,
but also because it is really true that you will learn so much more by
finding the answer yourself.
 
A

Alessio

Noob ha scritto:
Have you tried the following?
buf[21] = 0;

No, because I want to use printf.
No, because I want string to be 20 and not 21 chars len.
No, because using that, I must to check if string len is more than 21 chars.

Alessio.
 
A

Alessio

Ben Bacarisse ha scritto:
Look at whatever documentation you have for printf. See what you can
have in a %s format specifier other than just the % and the s. One of
these things will give you the answer!

I am being cryptic, in part, because this is the season of coursework,
but also because it is really true that you will learn so much more by
finding the answer yourself.

Thank you.
I assure you that I ALWAYS check documentation\google.
But today is different, I was too lazy!

Sorry,
Alessio.
 
E

Eric Sosman

Alessio said:
Hi,


there's a way to truncate a string using printf ?
For example, I've a string that is 50 chars long and I want to output to
video only first 20 chars.

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

("%20.20s" and "%-20.20s" may also merit consideration.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top