C to C++ statement conversion

Discussion in 'C++' started by CPlusPlus, Apr 8, 2010.

  1. CPlusPlus

    CPlusPlus Guest

    How can this C statement be converted to C++?



    printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    devInquiry.inq.dev.prodID);

    cout << ?????


    In case, someone is wondering, what is prodID. It is member of
    structure, e.g, as shown;


    struct DEV_INQ
    {
    // data members
    ...
    unsigned char prodID[16];
    // some more stuff here
    ...
    }DEVINQ;



    Thanks
    CPlusPlus, Apr 8, 2010
    #1
    1. Advertising

  2. CPlusPlus

    Jonathan Lee Guest

    On Apr 8, 2:03 pm, CPlusPlus <> wrote:
    > How can this C statement be converted to C++?
    >
    > printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > devInquiry.inq.dev.prodID);


    Use ostream::write().

    Or, if you really want the operator<< notation,
    construct a string from your unsigned char array.

    (You may just want to make prodID a string instead of
    a fixed size array. Depends on your needs, I guess.)

    --Jonathan
    Jonathan Lee, Apr 8, 2010
    #2
    1. Advertising

  3. CPlusPlus

    Ian Collins Guest

    On 04/ 9/10 06:03 AM, CPlusPlus wrote:

    > How can this C statement be converted to C++?
    >
    > printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > devInquiry.inq.dev.prodID);


    With or without the bug?

    --
    Ian Collins
    Ian Collins, Apr 8, 2010
    #3
  4. CPlusPlus

    CPlusPlus Guest

    On Apr 8, 5:27 pm, Ian Collins <> wrote:
    > On 04/ 9/10 06:03 AM, CPlusPlus wrote:
    >
    > > How can this C statement be converted to C++?

    >
    > > printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > > devInquiry.inq.dev.prodID);

    >
    > With or without the bug?
    >
    > --
    > Ian Collins


    Umm w/o the bug :D
    CPlusPlus, Apr 9, 2010
    #4
  5. CPlusPlus

    Ian Collins Guest

    On 04/ 9/10 01:22 PM, CPlusPlus wrote:
    > On Apr 8, 5:27 pm, Ian Collins<> wrote:
    >> On 04/ 9/10 06:03 AM, CPlusPlus wrote:
    >>
    >>> How can this C statement be converted to C++?

    >>
    >>> printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    >>> devInquiry.inq.dev.prodID);

    >>
    >> With or without the bug?

    >
    > Umm w/o the bug :D


    In that case, the answer's your compiler! printf is legal C++...

    --
    Ian Collins
    Ian Collins, Apr 9, 2010
    #5
  6. CPlusPlus

    tonydee Guest

    On Apr 9, 3:03 am, CPlusPlus <> wrote:
    > How can this C statement be converted to C++?
    >
    > printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > devInquiry.inq.dev.prodID);
    >
    > cout << ?????
    >
    >    ...
    >    unsigned char prodID[16];


    That's tricky. The .* in the printf format implies prodID might not
    be NUL terminated, so we'd need to do a character-by-character search
    for the first NUL, but not searching past 16 characters. Off the top
    of my head:

    void* p = devInquiry.inq.dev.prodID;
    size_t size = sizeof devInquiry.inq.dev.prodID;

    cout << " ";
    void* p_nul = memchr(p, '\0', size);
    cout.write(p, p_nul ? p_nul - p : size);
    cout << '\n';

    Which - pretty clearly I'd say - is worse than printf(). You could
    try the boost format library, which uses a printf-inspired format
    notation but adds type safety and other improvements....

    Cheers,
    Tony
    tonydee, Apr 9, 2010
    #6
  7. CPlusPlus

    CPlusPlus Guest

    On Apr 8, 11:43 pm, tonydee <> wrote:
    > On Apr 9, 3:03 am, CPlusPlus <> wrote:
    >
    > > How can this C statement be converted to C++?

    >
    > > printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > > devInquiry.inq.dev.prodID);

    >
    > > cout << ?????

    >
    > >    ...
    > >    unsigned char prodID[16];

    >
    > That's tricky.  The .* in the printf format implies prodID might not
    > be NUL terminated, so we'd need to do a character-by-character search
    > for the first NUL, but not searching past 16 characters.  Off the top
    > of my head:
    >
    >     void* p = devInquiry.inq.dev.prodID;
    >     size_t size = sizeof devInquiry.inq.dev.prodID;
    >
    >     cout << "  ";
    >     void* p_nul = memchr(p, '\0', size);
    >     cout.write(p, p_nul ? p_nul - p : size);
    >     cout << '\n';
    >
    > Which - pretty clearly I'd say - is worse than printf().  You could
    > try the boost format library, which uses a printf-inspired format
    > notation but adds type safety and other improvements....
    >
    > Cheers,
    > Tony


    Alright!
    CPlusPlus, Apr 9, 2010
    #7
  8. CPlusPlus

    CPlusPlus Guest

    On Apr 9, 8:44 am, CPlusPlus <> wrote:
    > On Apr 8, 11:43 pm, tonydee <> wrote:
    >
    >
    >
    >
    >
    > > On Apr 9, 3:03 am, CPlusPlus <> wrote:

    >
    > > > How can this C statement be converted to C++?

    >
    > > > printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > > > devInquiry.inq.dev.prodID);

    >
    > > > cout << ?????

    >
    > > >    ...
    > > >    unsigned char prodID[16];

    >
    > > That's tricky.  The .* in the printf format implies prodID might not
    > > be NUL terminated, so we'd need to do a character-by-character search
    > > for the first NUL, but not searching past 16 characters.  Off the top
    > > of my head:

    >
    > >     void* p = devInquiry.inq.dev.prodID;
    > >     size_t size = sizeof devInquiry.inq.dev.prodID;

    >
    > >     cout << "  ";
    > >     void* p_nul = memchr(p, '\0', size);
    > >     cout.write(p, p_nul ? p_nul - p : size);
    > >     cout << '\n';

    >
    > > Which - pretty clearly I'd say - is worse than printf().  You could
    > > try the boost format library, which uses a printf-inspired format
    > > notation but adds type safety and other improvements....

    >
    > > Cheers,
    > > Tony

    >
    > Alright!- Hide quoted text -
    >
    > - Show quoted text -


    I did this and it seems to be working on 32 and 64 bit machines:


    cout.write((char*)static_cast<UCHAR*>(devInquiry.inquiry.devType.productId),
    sizeof(devInquiry.inquiry.devType.productId));
    CPlusPlus, Apr 9, 2010
    #8
  9. CPlusPlus

    James Kanze Guest

    On Apr 8, 7:12 pm, Jonathan Lee <> wrote:
    > On Apr 8, 2:03 pm, CPlusPlus <> wrote:


    > > How can this C statement be converted to C++?


    > > printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > > devInquiry.inq.dev.prodID);


    > Use ostream::write().


    Which will have completely different semantics.

    > Or, if you really want the operator<< notation,
    > construct a string from your unsigned char array.


    That's the easiest solution:

    std::cout << std::string(devInquiry.inq.dev.prodId,
    devInquiry.inq.dev.prodId +

    sizeof(devInquiry.inq.dev.prodId)).c_str()
    << '\n';

    But I think it's still a workaround for bad initial design
    (which he may not be able to change now): if
    devInquiry.inq.dev,prodId is a null terminated C style string,
    it should be char, not unsigned char, and it should be null
    terminated. And if it's not, then using printf and "%s" to
    output it is wrong.

    --
    James Kanze
    James Kanze, Apr 9, 2010
    #9
  10. CPlusPlus

    James Kanze Guest

    On Apr 9, 1:47 pm, CPlusPlus <> wrote:
    > On Apr 9, 8:44 am, CPlusPlus <> wrote:
    > > On Apr 8, 11:43 pm, tonydee <> wrote:


    [...]
    > > > > printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > > > > devInquiry.inq.dev.prodID);


    > > > > cout << ?????


    > > > > ...
    > > > > unsigned char prodID[16];


    [...]
    > I did this and it seems to be working on 32 and 64 bit machines:


    > cout.write((char*)static_cast<UCHAR*>(devInquiry.inquiry.devType.productId),
    > sizeof(devInquiry.inquiry.devType.productId));


    Except that this doesn't have the same semantics as your
    original code.

    --
    James Kanze
    James Kanze, Apr 9, 2010
    #10
  11. CPlusPlus

    Jorgen Grahn Guest

    On Thu, 2010-04-08, CPlusPlus wrote:
    > How can this C statement be converted to C++?
    >
    >
    >
    > printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
    > devInquiry.inq.dev.prodID);
    >
    > cout << ?????
    >
    >
    > In case, someone is wondering, what is prodID. It is member of
    > structure, e.g, as shown;
    >
    >
    > struct DEV_INQ
    > {
    > // data members
    > ...
    > unsigned char prodID[16];
    > // some more stuff here
    > ...
    > }DEVINQ;


    Make prodID a class instead of an array, and provide an
    ostream << ProdId. Then you can implement the formatting
    *once* instead of every time you want to print it.

    I sometimes think the ostream width/padding/etc formatting
    manipulators are a red herring. ostream << Foo works best when it's up
    to Foo to format itself, exactly as you want it.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Apr 10, 2010
    #11
    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. Replies:
    3
    Views:
    800
  2. Jay McGavren
    Replies:
    11
    Views:
    1,127
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,076
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    460
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,050
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page