04-27-2016 03:44 PM - edited 04-27-2016 03:45 PM
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.
Solved! Go to Solution.
04-28-2016 01:00 AM
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).
05-01-2016 10:03 AM
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.