LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Questions Regarding to flexibly determine the array size of data storage

Solved!
Go to solution

I try to use CVI function below to load some datas from .csv file to the array I intended to use later:

filereturnvalue = FileSelectPopup ("c:\\Users\\Desktop\\FileDirectory", "*.csv", "", "Select Data File", VAL_LOAD_BUTTON, 0, 0, 1, 0, LoadFilePath); 

FileToArray (LoadFilePath, DATAArray, VAL_CHAR, MaxDataSize, 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_COLUMNS, VAL_ASCII); 

For "predetermined" data array, this is quite straight forward, because the size of array is already set.  However, I wonder if there is anyway I could use CVI directly import datas and determine the size of array on the fly?  Which means user don't need to know the length of data, and it could be determined while importing the file.

0 Kudos
Message 1 of 3
(3,405 Views)
Solution
Accepted by topic author Silverwolfman

Hello,

 

for ASCII files you can do so by first reading the file and counting newline characters, something like:

 

    int file_handle;
    int index;
    int size = 0;
    char line_buffer [ 80 ];
   

    if ( OpenFile ( ...) > 0 )
    {
        while ( ReadLine (...) > 0 )
        {
            size ++;
        }
    }  
    CloseFile ( file_handle );       

 

This way you can determine how many rows you have, but you need to have an idea of how long one line may be, just one number per line or 32000 because you need to specify the size of your line_buffer.

In the next step you would analyze one line for the occurrence of your column separator (say a ; ) to determine the number of columns (separators found + 1).

Message 2 of 3
(3,391 Views)

As always, Wolfgang has produced a rigorous and reliable solution. A quick and dirty method would be to use GetFileInfo to get the file size, if you can somehow reliably calculate the number of elements from the filesize.

0 Kudos
Message 3 of 3
(3,327 Views)