LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Stopping a while-loop at end-of-file

Hello.

I'm reading a file in one line at a time in a while-loop,  and I would like to run the loop until the end of the file is encountered, without throwing an error. Is there a way to check to see if the next line read in is the end of the file?

Thanks!

Ryan Hannebaum
Campus Ambassador
National Instruments
ELP Intern '06
0 Kudos
Message 1 of 12
(15,218 Views)
Hi Ryan
 
Let us know the LV version you're using. Since LV8.0 it works differnt than before.
 
Thomas
Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
0 Kudos
Message 2 of 12
(15,212 Views)
8.20, sorry!
0 Kudos
Message 3 of 12
(15,190 Views)

Hi,

Checkout this knowledge base entry.

Tunde

0 Kudos
Message 4 of 12
(15,162 Views)

Hi Ryan

I don't know if this is exactly what you are looking for, but the attached vi uses errorcode 4 to terminate the reading of the file. If you use this method, don't forget to check "Read Lines" (right click read from text file.vi, see picture).

Hope this helps.

Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
Download All
Message 5 of 12
(15,153 Views)

In text based languages like VB, or VC++, there is a boolean called EOF that signifies if the file pointer has reached end of file.  This can be used to end a loop.  I have just put in a suggestion to NI to include this functionality in all functions that reads from a file.  It should work like this.  After reading characters from a file, if the file pointer is now at end of file, a boolean EOF output of the Read File vi should become true, otherwise it should be false.  The EOF output can be used to stop a loop.  Very useful.

Here is a scenario:  I read 1 character at a time from a file in a loop.  In some iteration the file pointer is at the last character.  At that time, EOF output from Read File is false, no errors.  Then in the next loop iteration, I call Read File to read 1 character.  EOF becomes true and the loop stops, no errors.  If I would have requested to read more than one character, the file pointer would be past the end of file (theoretically), and EOF would be true and also an error would be generated signifying that I tried to go past EOF.  Hopefully, NI will incorporate this functionality in the next LV version.

- tbob

Inventor of the WORM Global
0 Kudos
Message 6 of 12
(15,138 Views)
Exactly! I'm actually a computer scientist by trade, brought up on Java and C++, so an EoF character was really what I was hoping for. In any case, as long as Error 4 isn't fatal, my program should survive.
0 Kudos
Message 7 of 12
(15,135 Views)

This is my workaround to avoid any errors:

Before the read loop, I use the EOF function to return the value of the end of file location.

On each loop iteration, I compare the Offset output from Read File, or Mark After Read output from Read Lines from File to the EOF value from above.

If marker is greater than or equal to EOF, exit the loop.  You can still get an error if attempting to read past EOF though.  Trap the error by checking the code value from the error out cluster.  Use a case structure to clear the error out if the error code is equal to Error 4.

- tbob

Inventor of the WORM Global
0 Kudos
Message 8 of 12
(15,128 Views)

Hmm....

That sounds as if you were describing my vi. I wonder if my attachments were looked at.... 😉

Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
0 Kudos
Message 9 of 12
(15,100 Views)

Hi,

 

following your sample, I made the following vi. I want each line to be entered as elements in the array. but somehow that s not happening.. not able to figure out where i m going wrong. can you help?

 

using NI LV 8.6

 

thanks.

Download All
0 Kudos
Message 10 of 12
(10,174 Views)