[freetds] Rows truncated at 176 chars when using CT-Lib interface

Alejandro Guerrieri alejandro.guerrieri at gmail.com
Mon Apr 27 10:49:28 EDT 2009

This is my first post to the list. I've started playing with FreeTDS a few
days ago, and I'm having some issues getting more than 176 characters back
using the ct-lib interface.

The code fragment I'm pasting below works fine when the data does not exceed
176 bytes, but anything over that value gets truncated. The fields I'm
retrieving are VARCHAR(255) and VARCHAR(4000) (it doesn't matter the size,
the problem occurs anyways).

Maybe I'm doing something really stupid (these are my first attempts at
coding with FreeTDS and ct-lib, and I found Sybase docs a little cryptic on
the "examples" side).

I'm using Mac OSX Leopard with gcc 4.0.1. and FreeTDS 0.82 (tried with the
MacPorts version and also compiling it myself from code downloaded from the
FreeTDS site. Same results).

Does anybody knows what I'm doing wrong? I don't believe this is a bug, I
guess somebody would have noticed before me (I did my homework and googled
around for similar problems, to no avail).

Thank you in advance,



struct data_s {

  CS_CHAR *data;

  CS_DATAFMT format;

  CS_INT size;



struct data_s *data;


while((ret = ct_results(conn->command, &res_type)) == CS_SUCCEED)


  switch (res_type)



    if (ct_res_info(conn->command, CS_NUMDATA, (CS_INT *)&columns,


      error(0, "Error fetching attributes");

      return -1;


    data = malloc(sizeof(struct data_s)*columns);

    for (i = 0; i < columns; i++) {

      if (ct_describe(conn->command, i+1, &data[i].format) != CS_SUCCEED) {

        error(0, "Error fetching column description");


        return -1;



      data[i].data = malloc(data[i].format.maxlength);

      data[i].size = data[i].format.maxlength;

      data[i].format.format = CS_FMT_NULLTERM;

      ct_bind(conn->command, i+1, &data[i].format, &data[i].data,

        &data[i].size, &data[i].ind);

      fprintf(stderr, "Col %d\tSize: %d\tMaxlen %d\n", i,

        data[i].size, data[i].format.maxlength);


    while(((ret = ct_fetch(conn->command, CS_UNUSED, CS_UNUSED,

      CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) {

      if( ret == CS_ROW_FAIL ) {

        fprintf(stderr, "** Error on row %d in this fetch batch.\n",

      } else {

      for (i=0;i<columns;i++) {

        fprintf(stdout, "[%d:%d:%s]\n", i,

          strlen((char *)&data[i].data), &data[i].data);




  case ...

More information about the FreeTDS mailing list