You are not logged in.

#1 2009-04-07 20:22:45

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

[SOLVED] C++ print_buffer and truncating strings

I am editing a C++ function call to print_buffer. I haven't worked with C++ before so I presume it is a standard C++ function because it isn't getting defined in the program anywhere.

What I need is to truncate a string to 50 characters and left justify it. What I got was that all strings smaller than 50 characters had spaces properly appended, but strings longer were not truncated at all.

This is the relevant code, which is from rtorrent actually. It used to say just "%s" instead of "%-50s"

first = print_buffer(first, last, "%-50s", d->download()->name().c_str());

So did I misunderstand how print_buffer works? How would this work with sprintf for that matter? Does anyone know a function I can call on d->download()->name().c_str() to truncate it?

Last edited by Procyon (2009-04-07 20:59:18)

Offline

#2 2009-04-07 20:32:10

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] C++ print_buffer and truncating strings

I have no idea what print_buffer is, but it looks like it's taking a regular snprintf format string - maybe try "%-50.50s"

Offline

#3 2009-04-07 20:59:06

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] C++ print_buffer and truncating strings

That worked, thanks!

Now I got rtorrent to display all info per item on 1 line instead of 3.

Offline

Board footer

Powered by FluxBB