Pro*C Question - printing varchar

K

Kareem Nutt

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?
 
B

Barry Schwarz

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>>
 
C

CBFalconer

Kareem said:
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.
 
D

Dave Thompson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top