By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

string format question

P: n/a
I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.

Christian

Sep 21 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a

christian.bongio...@gmail.com wrote:
I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
for loop + strlen?

Tom

Sep 21 '06 #2

P: n/a
On Thu, 21 Sep 2006, Tom St Denis wrote:
>
christian.bongio...@gmail.com wrote:
>I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.

for loop + strlen?
You don't need either of them. Just do:

sprintf(buff, "%-31s", value);
buff[31] = ' ';

Tak-Shing
Sep 21 '06 #3

P: n/a
Tak-Shing Chan wrote:
On Thu, 21 Sep 2006, Tom St Denis wrote:

christian.bongio...@gmail.com wrote:
I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
for loop + strlen?

You don't need either of them. Just do:

sprintf(buff, "%-31s", value);
buff[31] = ' ';

Tak-Shing
I think if the "value" is >= 32 characters, NO padding is wanted, so
this doesn't seem to meet OP's requirements.

I'd say that writing his own function to do this is the proper way.

This would work. It isn't tested, and the strncpy is wasteful, but...

strncpy(buff, value, 32);

if (buf[31] == '\0') {
size_t len = strlen(buf);
memset(buf+len, ' ', 32-len);
}

-David

Sep 21 '06 #4

P: n/a
On Thu, 21 Sep 2006, David Resnick wrote:
Tak-Shing Chan wrote:
>On Thu, 21 Sep 2006, Tom St Denis wrote:
>>>
christian.bongio...@gmail.com wrote:
I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.

for loop + strlen?

You don't need either of them. Just do:

sprintf(buff, "%-31s", value);
buff[31] = ' ';

Tak-Shing

I think if the "value" is >= 32 characters, NO padding is wanted, so
this doesn't seem to meet OP's requirements.

I'd say that writing his own function to do this is the proper way.

This would work. It isn't tested, and the strncpy is wasteful, but...

strncpy(buff, value, 32);

if (buf[31] == '\0') {
size_t len = strlen(buf);
memset(buf+len, ' ', 32-len);
}
If ``value'' is ever going to exceed 31 characters, then one
could simply use sscanf followed by memset:

int len = 0;
sscanf(value, "%32c%n", buff, &len);
memset(buff + len, ' ', 32 - len);

Tak-Shing
Sep 21 '06 #5

P: n/a
Tak-Shing Chan wrote:
On Thu, 21 Sep 2006, David Resnick wrote:
Tak-Shing Chan wrote:
On Thu, 21 Sep 2006, Tom St Denis wrote:
christian.bongio...@gmail.com wrote:
I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.

for loop + strlen?

You don't need either of them. Just do:

sprintf(buff, "%-31s", value);
buff[31] = ' ';

Tak-Shing
I think if the "value" is >= 32 characters, NO padding is wanted, so
this doesn't seem to meet OP's requirements.

I'd say that writing his own function to do this is the proper way.

This would work. It isn't tested, and the strncpy is wasteful, but...

strncpy(buff, value, 32);

if (buf[31] == '\0') {
size_t len = strlen(buf);
memset(buf+len, ' ', 32-len);
}

If ``value'' is ever going to exceed 31 characters, then one
could simply use sscanf followed by memset:

int len = 0;
sscanf(value, "%32c%n", buff, &len);
memset(buff + len, ' ', 32 - len);

Tak-Shing
Yes, that's another way to do it. I don't use the *scanf functions
very much at all, didn't occur to me. Honestly, a simple loop to
copy the thing would be about as good.

Now that I reread the OP's post, he wanted a 1 line answer. I don't
see that that is so easy to do, unless the "line" is a bizarre mess.

-David

Sep 21 '06 #6

P: n/a
ch*****************@gmail.com wrote:
I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
I found this to work in a number of implementations:
sscanf(value, "%32c", (char*)memset(buf, ' ', 32));

I don't think the above is conforming, though. The %32c match should
not succeed if 'value' contains less than 32 characters.

A better solution would be:
if(!sscanf(value, "%32c", (char*)memset(buf, ' ', 32)))
memcpy(buf, value, strlen(value));

And if you really need a one-liner you can use || instead of the 'if'.

Sep 21 '06 #7

P: n/a
On 21 Sep 2006 08:51:08 -0700, ch*****************@gmail.com wrote:
>I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
If it is not to be nul terminated, it is not a string. So first make
up your mind. Do you want a string or do you want an (unterminated)
array of char. If the latter, what do you want done with any unused
elements of buff? Do you want value left or right (or other)
justified in buff? Is value truly a string or at least a pointer to
one?
Remove del for email
Sep 22 '06 #8

P: n/a
On Thu, 21 Sep 2006, Dingo wrote:
ch*****************@gmail.com wrote:
>I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars

with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.

I found this to work in a number of implementations:
sscanf(value, "%32c", (char*)memset(buf, ' ', 32));

I don't think the above is conforming, though. The %32c match should
not succeed if 'value' contains less than 32 characters.

[snip]
Good observation. Incidentally, this means that my
``solution'' upthread is broken. C90 is a bit ambiguous with
regards maximum field width, but C99 makes it absolutely clear
that "%32c" is matching exactly 32 characters, no more, no less.

Nice one-liner, by the way.

Tak-Shing
Sep 22 '06 #9

P: n/a
On Thu, 21 Sep 2006, David Resnick wrote:
Tak-Shing Chan wrote:
> If ``value'' is ever going to exceed 31 characters, then one
could simply use sscanf followed by memset:

int len = 0;
sscanf(value, "%32c%n", buff, &len);
memset(buff + len, ' ', 32 - len);

Tak-Shing

Yes, that's another way to do it. I don't use the *scanf functions
very much at all, didn't occur to me. Honestly, a simple loop to
copy the thing would be about as good.

Now that I reread the OP's post, he wanted a 1 line answer. I don't
see that that is so easy to do, unless the "line" is a bizarre mess.
As it turns out, my sscanf call is broken (so are a large
number [1] of C90 implementations). Dingo has a one-liner
downthread which you may find interesting.

Tak-Shing

[1] This probably means that full C90 portability is but a myth.
I hereby invite comp.lang.c to report on the return value of
sscanf("too short", "%32c", buff)---invoke your compiler in
conforming C90 mode and see if it really conforms.
Sep 22 '06 #10

P: n/a
ch*****************@gmail.com wrote:
I need to format a string and I would like to do it in 1 line

char buff[32];

I need something LIKE:

sprintf(buff,"[%-32s]",value); // space pad up to 32 chars
What is value?

Either way, that will necessarily print more than 32 characters
irrespective
of the null terminator. So is it really the case that you want to
output a buffer
that need not be null terminated? If so...

char buffer[32] = "hello6789|123456789|123456789|32";
int len = 5;

printf("buffer: >%.32s<\n", buffer);
printf("buffer: >%-32.*s<\n", len, buffer);
printf("buffer: >%32.*s<\n", len, buffer);
with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don't know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
Have you considered the possibility of simply making your buffer
33 chars wide and ignoring the last byte? Or using a temporary
buffer and then memcpy the result? What about rolling your own
strncpy style function that pads with spaces rather than null bytes?

Do you have a more concrete example of what you're trying to do?

--
Peter

Sep 22 '06 #11

P: n/a
clever use of parameter passing subtleties. That trick gets a +5

Christian

Sep 22 '06 #12

P: n/a
ch*****************@gmail.com wrote:
clever use of parameter passing subtleties. That trick gets a +5
What use? What trick?

Please quote a sufficient amount of the previous article to give your
post context. Google does that automatically now.

Brian
Sep 22 '06 #13

P: n/a
ch*****************@gmail.com wrote:
>
clever use of parameter passing subtleties. That trick gets a +5
Meaningless without proper quoting. Google is not usenet, it is
only a flawed interface to the usenet system. All articles should
stand by themselves.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 23 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.