473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sprintf on MVS

I am trying to pad a string with leading character zeros. There seems
to be a difference between the behavior of sprintf on Windows
(Microsoft Visual C++ .NET) and on MVS. Can anyone explain the reason
for this? Or am I doing something incorrectly? Thanks in advance!

---
On Windows:
---

string s = "BB";
char buf[5];
sprintf(buf, "%04s", s.c_str());

// after sprintf: buf = "00BB" <========!!!

---
On MVS:
---

string s = "BB";
char buf[5];
sprintf(buf, "%04s", s.c_str());

// after sprintf: buf = " BB" <========!!!

Nov 14 '05 #1
3 2209
In article <11**********************@f14g2000cwb.googlegroups .com>,
<po***********@yahoo.com> wrote:
I am trying to pad a string with leading character zeros. There seems
to be a difference between the behavior of sprintf on Windows
(Microsoft Visual C++ .NET) and on MVS. Can anyone explain the reason
for this? Or am I doing something incorrectly? Thanks in advance!
Well, the first thing you are doing wrong is asking the question
on a C newsgroup instead of a C++ newsgroup ;-)
---
On Windows:
--- string s = "BB";
C has no 'string'.
char buf[5];
sprintf(buf, "%04s", s.c_str());
In C, that would imply that s is a structure (or union) with a member
named c_str which is a function with no parameters. But you can't
store functions in C, only function pointers, so you certainly
wouldn't be getting anything useful there. And if s *were*
a structure or union, you wouldn't be able to initialize it with
a character array...
// after sprintf: buf = "00BB" <========!!! ---
On MVS:
--- string s = "BB";
char buf[5];
sprintf(buf, "%04s", s.c_str()); // after sprintf: buf = " BB" <========!!!


In C, the meaning of a 0 modifier on an s parameter is undefined.
So in C, either output would be within the bounds of the standard.
Along with lots of other possibilities such as putting in umaults
intead of spaces or 0's.
Try this:

char s[] = "BB";
char buf[5];
sprintf(buf, "%s%s", "0000" + min(4,strlen(s)), s);

--
"Mathematics? I speak it like a native." -- Spike Milligan
Nov 14 '05 #2
Thanks for the reply!

I did not realize that a 0 modifier on an s parameter is undefined - i
will not use it.

Sorry for including a c++ construct (string) on this newsgroup. My
intention was not to imply that s is a struct or union, etc., etc.

Thanks again.

Nov 14 '05 #3
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11**********************@f14g2000cwb.googlegroups .com>,
<po***********@yahoo.com> wrote:

[snip]
string s = "BB";


C has no 'string'.
char buf[5];
sprintf(buf, "%04s", s.c_str());


In C, that would imply that s is a structure (or union) with a member
named c_str which is a function with no parameters. But you can't
store functions in C, only function pointers, so you certainly
wouldn't be getting anything useful there.


No, it implies that s is a structure or union with a member named
c_str which is a *pointer* to a function with no parameters.
Remember, the C function call operator takes a pointer-to-function as
it first operand. In the common case of using a function name, such
as foo(10, 20), the function name foo is implicitly converted to a
pointer-to-function value before it becomes the operand of the call
operator.

<OT>
The difference between this and a call to a C++ member function is
that there's no implicit parameter referring to the object containing
the pointer. s.c_str() is perfectly legal in C, assuming s is
declared properly, but the function pointed to by c_str has no
knowledge of the value of s -- which is why you don't see this kind of
thing as often in C as in C++. Something like s.c_str(s) would be
closer to the C++ semantics.
</OT>

This is, of course, a tangent of no particular relevance to what the
OP was actually asking about.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
3
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf,...
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
5
by: Dave | last post by:
Hi, In awk I can do this: var1="x"; temp = sprintf("Variable 1: %s Variable 2: %%s", var1); # now the value of temp is "Variable 1: x Variable 2: %s" var2="y"; printf(temp,var2);
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.