string format question

  • Thread starter christian.bongiorno
  • Start date
C

christian.bongiorno

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
 
T

Tom St Denis

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
 
T

Tak-Shing Chan

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
 
D

David Resnick

Tak-Shing Chan said:
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
 
T

Tak-Shing Chan

Tak-Shing Chan said:
(e-mail address removed) 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
 
D

David Resnick

Tak-Shing Chan said:
Tak-Shing Chan said:
On Thu, 21 Sep 2006, Tom St Denis wrote:


(e-mail address removed) 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
 
D

Dingo

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'.
 
B

Barry Schwarz

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
 
T

Tak-Shing Chan

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
 
T

Tak-Shing Chan

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.
 
P

Peter Nilsson

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?
 
D

Default User

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
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top