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