Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sprintf on MVS

Reply
Thread Tools

sprintf on MVS

 
 
portergrouptx@yahoo.com
Guest
Posts: n/a
 
      05-29-2005
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" <========!!!

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-29-2005
In article < .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
 
Reply With Quote
 
 
 
 
portergrouptx@yahoo.com
Guest
Posts: n/a
 
      05-29-2005
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-29-2005
(Walter Roberson) writes:
> In article < .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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MVS - Cobol-Java Out of Memory Issue SK Java 4 08-09-2005 07:37 PM
python for MVS legacy access? asdf sdf Python 0 04-27-2004 10:34 AM
ftruncate() on MVS datasets. John Fly C Programming 2 04-06-2004 12:53 PM
MVS Retraining Steve Schooler Java 1 01-16-2004 01:32 AM
USS read MVS datasets Mark Java 2 06-29-2003 12:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57