Pro*C Question - printing varchar

Discussion in 'C Programming' started by Kareem Nutt, Oct 15, 2004.

  1. Kareem Nutt

    Kareem Nutt Guest

    I have the following:

    varchar name[21];
    .... select statement puts value into name ...
    .....
    name.arr[name.len] = '\0';
    printf ("name is %s.\n", name.arr);

    Example of what I get when I do this I get:
    "name is bob."

    What I want is to have all the extra spaces printed out as well:
    "name is bob."

    Anyone know how to do this?
    Kareem Nutt, Oct 15, 2004
    #1
    1. Advertising

  2. On Thu, 14 Oct 2004 22:09:10 -0400, Kareem Nutt <>
    wrote:

    >I have the following:
    >
    >varchar name[21];
    >... select statement puts value into name ...
    >....
    >name.arr[name.len] = '\0';
    >printf ("name is %s.\n", name.arr);
    >
    >Example of what I get when I do this I get:
    >"name is bob."
    >
    >What I want is to have all the extra spaces printed out as well:
    >"name is bob."
    >
    >Anyone know how to do this?


    What extra spaces? If you don't like

    printf("name is %s.\n", name.arr);

    then you have to tell us where the extra spaces come from.


    <<Remove the del for email>>
    Barry Schwarz, Oct 15, 2004
    #2
    1. Advertising

  3. Kareem Nutt

    CBFalconer Guest

    Kareem Nutt wrote:
    >
    > I have the following:
    >
    > varchar name[21];
    > ... select statement puts value into name ...
    > ....
    > name.arr[name.len] = '\0';


    name is an array of the undefined varchar. Supply that
    definition. name.arr is an error.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 15, 2004
    #3
  4. On Thu, 14 Oct 2004 22:09:10 -0400, Kareem Nutt <>
    wrote:

    > I have the following:
    >
    > varchar name[21];
    > ... select statement puts value into name ...
    > ....
    > name.arr[name.len] = '\0';


    Assuming that 'varchar' declaration actually expanded to a struct
    containing some_integer_type len and char arr[21 or maybe 22], as I
    have seen for another SQL-binding-C comparable to Pro*C, and
    assuming the select (or fetch?) sets name.len to the actual value
    length, that is without/before padding, this truncates the string at
    the location of the first possible padding space.

    > printf ("name is %s.\n", name.arr);
    >
    > Example of what I get when I do this I get:
    > "name is bob."
    >
    > What I want is to have all the extra spaces printed out as well:
    > "name is bob."
    >
    > Anyone know how to do this?


    Do you really mean leading spaces and not trailing? If there are
    leading spaces no good SQL implementation should remove them, and
    neither would the code you posted.

    If you meant trailing padding, to make values fixed-length:

    *If* Pro*C returned padding spaces, don't clobber them as above. If it
    did (allocate room for and) store a null terminator after the last
    data/padding char, just use %s and name.arr. If not use %.Ns where N
    is the number of actual char, or * and add that number as an argument:
    printf ("%name is %.*s\n", (int) sizeof name.arr, name.arr);

    If it (maybe) didn't store padding and (so) you have a variable-length
    null-terminated string, as normal for C, use %Ns to right-align
    (leading spaces) in a fixed-width field, or %*s with an additional int
    argument; or %-Ns to left-align (trailing spaces) or %-*s similarly.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Oct 21, 2004
    #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. Hazzard
    Replies:
    4
    Views:
    3,249
    Hazzard
    Jul 9, 2003
  2. luna
    Replies:
    1
    Views:
    13,795
    Kevin Spencer
    Feb 6, 2004
  3. Kenneth Keeley

    Shorten a SQL VarChar result.

    Kenneth Keeley, Oct 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    659
    Scott Mitchell [MVP]
    Oct 28, 2004
  4. =?Utf-8?B?U2lsZWVzaA==?=

    Urgent Converting varchar to ntext in SQL

    =?Utf-8?B?U2lsZWVzaA==?=, Nov 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    15,813
    =?Utf-8?B?U2lsZWVzaA==?=
    Nov 12, 2004
  5. Wayne Erfling
    Replies:
    0
    Views:
    313
    Wayne Erfling
    Nov 18, 2006
Loading...

Share This Page