07-31-2012 04:48 PM
Hi all,
I would like to take numbers from a text file and read them line by line. Eventually I'd like to store them in an array but for now how would I read each line individually and then move onto the next line? Thanks!
07-31-2012 04:58 PM
You can use the read from spreadhseet VI to read the file and return and array of your values.
07-31-2012 05:14 PM - edited 07-31-2012 05:15 PM
In addition to Mark's suggestion, if you truly want to read line by line, you can right click the read from text file VI and in the right click menu there is an option to do this. I believe reading the file all at once is more efficient though, probably due to memory allocations. If you only want specific lines and that is your main motivation for reading line by line, you could read the whole file, then discard the lines you don't care about.
07-31-2012 09:38 PM
@for(imstuck) wrote:
I believe reading the file all at once is more efficient though, probably due to memory allocations
This is true to a point. If the file is very large you should read it in chunks. Reading a large file (> 1MB) all at once will be very slow.
07-31-2012 11:20 PM
08-01-2012 09:30 AM
@for(imstuck) wrote:
Thanks for reminding me of this. I actually had a coworker who had to do this recently. I may also refactor some of my recent code to try and see if I can make it more efficient by doing this
It has been a while since I analyzed this and I am not certain that the 1 MB threshold is where you see performance hits. Some experimentation might be necessary to find the optimal point. In our code we took the easy approach and generally read in blocks of 10 to 20 KBs. This seems to give us good performance. Large blocks sizes may work as well but our timing didn't show significant differences between 1KB, 10 KB and 20KB block sizes. Disclaimer: What you do with those blocks could affect your performance but the execution time was basically the same in terms of simply reading the file.