Hi all,
Referencing “http://code.kx.com/wiki/Cookbook/InterfacingWithC”, I was able to create the following table via a 64bit c++ app;
stime rtime sym bidprice bidsize askprice asksize
-----------------------------------------------------------------------------..
06:14:25.847 06:14:25.882 ES 205175 69 205200 18
06:14:32.615 06:14:32.655 ES 205175 70 205200 18
06:14:33.265 06:14:33.305 ES 205175 71 205200 14
06:14:33.965 06:14:34.005 ES 205175 69 205200 19
06:14:38.015 06:14:38.055 ES 205175 69 205200 20
06:14:39.215 06:14:39.255 ES 205175 69 205200 21
However, when I try to access the data and manipulate it via c++ the following code does not work. Is the code in error or is using a 64bit app the 32bit app not possible?
#include<cstdio>
#include<cstdlib>
#include"k.h"
// obtain k.h from http://kx.com/q/c/c/k.h
// linux:
// compile with gcc -m64 -DKXVER=3 csv.c c.o
// obtain c.o from http://kx.com/q/l64/c.o for linux
// windows:
// start the x86 or 64 bit version of build environment, then: cl -DKXVER=3 /MD csv.c c.obj ws2_32.lib
// obtain c.obj from http://kx.com/q/w32/ or w64/
int main(int argc,char*argv)
{
int handle = khpu((S) “localhost”, 5000, (S) “myusername:mypassword”);
if (handle < 0)
{
printf(“Cannot connect\n”);
return 1;
}
else if (!handle)
{
printf(“Wrong credentials\n”);
return 1;
}
/*
Get a keyed table by executing a query.
*/
K x = k(handle, “select from quote”, (K)0);
/*
Convert the result to a simple table.
NB. x is no longer valid and has been deallocated.
*/
K y=ktd(x);
/*
Note that if the ktd conversion fails for any reason,
it returns 0 and x is not freed.
since 2011-01-27, ktd always decrements ref count of input.
*/
if (0 == y)
(void) puts (“x is still a keyed table because the conversion failed.”);
else
(void) puts (“y is a simple table and x has been deallocated.”);
K columnNames=kK(y->k)[0];
K columnData=kK(y->k)[1];
int nCols=columnNames->n; <<<<< cause seg fault
int nRows=kK(columnData)[0]->n;
for(int row=0;row<nRows;row++)
{
if(0==row)
{
for(int col=0;col<nCols;col++)
{
if(col>0)printf(“,”);
printf(“%s”,kS(columnNames)[col]);
}
printf(“\n”);
}
for(int col=0;col<nCols;col++)
{
K obj=kK(columnData)[col];
if(col>0)printf(“,”);
switch(obj->t)
{
case(0):{ // handle a list of char vectors
K x=kK(obj)[row];
if(10==x->t){int i;for(i=0;i<xn;i++)printf(“%c”,kG(x)[i]);}
else printf(“type %d not supported by this client”,obj->t);
}break;
case(1):{printf(“%d”,kG(obj)[row]);}break;
case(4):{printf(“%d”,kG(obj)[row]);}break;
case(5):{printf(“%d”,kH(obj)[row]);}break;
case(6):{printf(“%d”,kI(obj)[row]);}break;
case(7):{printf(“%lld”,kJ(obj)[row]);}break;
case(8):{printf(“%f”,kE(obj)[row]);}break;
case(9):{printf(“%f”,kF(obj)[row]);}break;
case(11):{printf(“%s”,kS(obj)[row]);}break;
default:{printf(“type %d not supported by this client”,obj->t);}break;
}
}
printf(“\n”);
}
kclose(handle);
return 0;
}
Thanks,
Oran