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 13 1892
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
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
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
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
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 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'.
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
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
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. 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
clever use of parameter passing subtleties. That trick gets a +5
Christian 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: san |
last post by:
Hello, all! I have question about String.Format method.
There are two variants: public static string Format(string, params
object); and public static string Format(IFormatProvider, string, params...
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: Tommy Vercetti |
last post by:
The first three of these statements work. The fourth doesn't.
String::Format("{0}{0}{0}{0}", S"");
String::Format("{0}{1}{0}{0}", S"", S"");
String::Format("{0}{1}{2}{0}", S"", S"", S"");...
|
by: nobody |
last post by:
I know that given a FormatString and a DateTime you can use
DateTime.ToString(...) to convert the DateTime to a String. My question is
how can you turn that around? Given a String and a...
|
by: Chris Dunaway |
last post by:
I have a table in the database with a phone number field. The phone number
is stored without any punctuation (e. g. 9995551234). I wish to take that
string and format it for display (e. g. (999)...
|
by: L. Scott M. |
last post by:
Have a quick simple question:
dim x as string
x = "1234567890"
-------------------------------------------------------
VB 6
dim y as string
|
by: AMP |
last post by:
Hello,
I want to add some variables to a string and this isnt working.
textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo";
What am I doing wrong?
Thanks
Mike
|
by: Scewbedew |
last post by:
Suppose I have the following code:
string myFormat = "Line1/nLine 2";
string formattedString = string.Format(myFormat);
....that would produce a 2-line output as expected.
But if I load...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |