LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you correctly use ini_getdata

Hi,
I'm having a bit of a problem using INI_getdata. 

Here is a snippet of the ini file:
----------------------------------------------------------------------------------------
[MOTOR GROUP SETTINGS]

    Number of Groups = 32

    Number of Axis Per Group = 3

----------------------------------------------------------------------------------------

Here is my 'test' code that I wrote to try and read this.  When I run this, I get "13" for the variable 'c'.  I should be getting "32".  What am I doing wrong?


void testIniRead(void)
{
    unsigned char *data;
    int c;
    int dataSize;
   
    data = malloc(sizeof(int));
   
    getVal("MOTOR GROUP SETTINGS", "Number of Groups", data, &dataSize);

    c = (int)*data;
   
}

int getVal(char *secName, char *tagStr, unsigned char *data, int * dataSize)
{

    return (Ini_GetData(iniTextStream, secName, tagStr, &data, (long *) dataSize));
}

----------------------------------------------------------------------------------------



0 Kudos
Message 1 of 2
(3,337 Views)
Hi,

There are a few issues with the code snippet you've posted.  First, it looks like you want c to hold the int value 32, but you can't just dereference and cast the string "32" as an int to make that happen:

c = (int)*data;

The dereference will just give you the first character in that string, and c will just end up storing the ascii code for that character.  In general, if you need to convert a string into an integer, you need to use a function like atoi.  In this case, however, you can just use the Ini_GetInt function to read "Number of Groups" directly into an integer.

Another problem is that getVal will never actually output the string returned by Ini_GetData.  Your getVal function's data parameter needs to be an unsigned char **, not just an unsigned char *.  Also, you do not need to dynamically allocate the local variable data in main (which incidentally is allocated as the wrong type, since it is declared as an unsigned char *) since Ini_GetData allocates the memory for you, but you do need to free the string returned by Ini_GetData.

Here is a snippet that shows how to get "Number of Groups" as well as a hypothetical string value "Some String":

void testIniRead(void)
{
    unsigned char *data;
    int c;
    int dataSize;

   
    Ini_GetInt(
iniTextStream, "MOTOR GROUP SETTINGS", "Number of Groups", &c);

    Ini_GetData(
iniTextStream, "MOTOR GROUP SETTINGS", "Some String", &data, &dataSize);
   
    /* use/copy data */

    free(data);
}

You should also take a look at the sample CVIXX\samples\toolbox\ini.prj for more details on how to use the INI functions.

Hope this helps.

Mert A.
National Instruments
Message 2 of 2
(3,321 Views)