sprintf on MVS

Discussion in 'C Programming' started by portergrouptx@yahoo.com, May 29, 2005.

  1. Guest

    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" <========!!!
     
    , May 29, 2005
    #1
    1. Advertising

  2. In article <>,
    <> 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
     
    Walter Roberson, May 29, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , May 29, 2005
    #3
  4. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > <> 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) <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.
     
    Keith Thompson, May 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark

    USS read MVS datasets

    Mark, Jun 28, 2003, in forum: Java
    Replies:
    2
    Views:
    2,412
    Jon A. Cruz
    Jun 29, 2003
  2. Steve Schooler

    MVS Retraining

    Steve Schooler, Jan 15, 2004, in forum: Java
    Replies:
    1
    Views:
    619
    Andrew Thompson
    Jan 16, 2004
  3. SK
    Replies:
    4
    Views:
    1,579
    jan V
    Aug 9, 2005
  4. John Fly

    ftruncate() on MVS datasets.

    John Fly, Apr 6, 2004, in forum: C Programming
    Replies:
    2
    Views:
    341
    Christopher Benson-Manica
    Apr 6, 2004
  5. asdf sdf

    python for MVS legacy access?

    asdf sdf, Apr 27, 2004, in forum: Python
    Replies:
    0
    Views:
    370
    asdf sdf
    Apr 27, 2004
Loading...

Share This Page