LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I allocate memory for excel strings

I would like to use the function ExcelRpt_GetCellValue to read a column of string values from an excel spreadsheet. I've tried the following:
 
   char **AColumnExcel = NULL;
   AColumnExcel = malloc(2000*sizeof(char *));
   for(index=0;index<2000;index++) AColumnExcel[index]=malloc(1000*sizeof(char));
   local_err = ExcelRpt_GetWorksheetFromIndex (workBookHandle, 1, &workSheetHandle);
   local_err = ExcelRpt_GetCellValue (workSheetHandle, "A1:A2000", ExRConst_dataString, AColumnExcel);
   CA_DiscardObjHandle(workSheetHandle);
It returns garbage. How am I supposed to allocate memory for this?
0 Kudos
Message 1 of 3
(4,996 Views)
Hi,

Here is the documentation of the function.

There is not enough information, but maybe, with data type char**, you don't have to allocate memory for strings.
I said so, because I remember about INI functions which needs char** type that user only have to free. (Space is allocated by the function ?)

Maybe you have already tested what I said. Maybe I'm just wrong.
0 Kudos
Message 2 of 3
(4,979 Views)
This is a fairly common mistake. In C, when passing an output parameter, you have to pass the address of a variable that can hold the output data. So in your case, if you're trying to get a string from the function, you would do this:

char *val = NULL;
local_err = ExcelRpt_GetCellValue (workSheetHandle, "A1", ExRConst_dataString, &val);


But you should note that this function only gets the value of a single cell. The function description makes that clear. Even though the second parameter is called "cell range," if you read the help, it clarifies that it should be a single cell, or else it will just return the value of the first cell in that range.

You should instead look at the ExcelRpt_ReadData function. You'll use it something like this:

char *AColumnExcel[2000];
local_err = ExcelRpt_ReadData(workSheetHandle, "A1:A2000", ExRConst_dataString, AColumnExcel
);

Note that for both of the functions, the strings returned are allocated by the function, but you are still responsible for freeing each one with a call to CA_FreeMemory:

for (i = 0; i < 2000; ++i)
   CA_FreeMemory(AColumnExcel[i]);


Mert A.
National Instruments


Message Edited by Mert A. on 07-31-2008 05:03 PM
Message 3 of 3
(4,964 Views)