LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

I need to read in float values from a file, what is the best way to do this? I control the format of the file.

Hello Arius,
It depends on your programming constraints and preferences. An ASCII data file is easy to read and modify for the end-user and other applications. However, binary files are generally smaller and faster to save and load. One method would be to write your own routine to read and write to the files using the ANSI C functions fprintf() and fscanf(). CVI also includes the FmtFile() and ScanFile() formatted-IO functions that have more advanced capabilities. However, if your data is all of the same datatype (double precision floating point values, for example), I would recommend trying the ArrayToFile() and FileToArray() functions, also included with CVI. These functions provide an easy means of inputting and outputting arrays of data.

Jeremiah Cox
Applicatio
ns Engineer
National Instruments
http://www.ni.com/ask
0 Kudos
Message 2 of 9
(5,002 Views)

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

0 Kudos
Message 3 of 9
(4,604 Views)

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.

0 Kudos
Message 4 of 9
(4,588 Views)
As I mentioned in my post, the data file is ASCII and I do not know the format, other than having one value per line. (I don't know how many digits follow the decimal point, for example.) It would be very helpful if FileToArray returned the number of values it actually converted. That way I could successively read in an expected quantity of values into a temporary array, then copy each part into a final array after reading them all. Oh well, maybe in a future release...
0 Kudos
Message 5 of 9
(4,573 Views)

OK, here is the way I would tackle the problem:

 

  • Select & open the file
  • Call ListCreate(sizeof(double)) 
  • for each line in the file:
    • call ReadLine()
    • call Scan() to get the value from the current line
    • if successful, call ListInsertItem(,,END_OF_LIST)
  • call ListNumItems() to get the number of values
  • call ListGetDataPtr() to obtain a pointer to the list data
  • when done with the data, call ListDispose()

 

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.

 

0 Kudos
Message 6 of 9
(4,560 Views)

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 ( ... );

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 7 of 9
(4,558 Views)

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.

0 Kudos
Message 8 of 9
(4,555 Views)
Sometimes brute force is the only option... Thanks for the tip.
0 Kudos
Message 9 of 9
(4,554 Views)