04-13-2001 09:35 AM
04-16-2001 09:42 AM
07-13-2009 02:02 PM
The CVI FileToArray function requires prior knowledge of how many values are in the file. Without knowing this or knowing what is the ASCII format of the values in the file (which would be needed to call fscanf), what is the simplest way of getting all the values into the array?
Thanks In Advance!
Jesse
07-13-2009 09:22 PM
Hello Jesse.
If you work with binary files, you can determine the total number of elements by calling GetFileSize(), then dividing the result by sizeof(datatype), where datatype is one of VAL_DOUBLE, VAL_FLOAT etc.
You can then dynamically allocate your array to hold the number of elements you require.
Regards,
Colin.
07-14-2009 10:46 AM
07-14-2009 03:24 PM
OK, here is the way I would tackle the problem:
The list grows dynamically to accommodate its data. You may want to impose a limit.
You will find the List* functions (well documented) in the CVI toolbox.
By the way:
It's good that (apparently) you searched the forum before posting. Given that your situation is different from that of Arius, it would have made sense to create a new thread.
Good luck.
07-14-2009 03:28 PM
Why not doing it the good old way? (pseudo-code only: don't forget to complete with error checking and all the usual stuff)
cnt = 0;
fileHandle = OpenFile ( ... );
// Read the file to count lines
while ((r = ReadLine ( ... )) != -2) {
if (r == -1) {
// Handle the I/O error
}
cnt++;
}
// Dynamically alocate memory
array = calloc (cnt, sizeof (double));
// Read the file again
SetFilePos ( ... ); // Rewind file
for (i = 0; i < cnt; i+) {
r = ReadLine ( ... );
if (r == -1) {
// Handle the I/O error
}
Scan ( ..., &array[i]);
}
CloseFile ( ... );
07-14-2009 04:18 PM
Re: The list grows dynamically to accommodate its data.
Ah, that should be helpful; thanks! I was going to try reading in a pre-determined quantity and re-allocate the buffer as the number of values grows. Having the buffer grow automatically is a great help! I've used the List functions in other contexts; it did not occur to me to use them for this.
And, yes I did search extensively (about 1/2 hour or so) before finding this thread. It seemed close enough to my problem, so I piggy-backed on it. You're right; I should have started a new thread.
07-14-2009 04:19 PM