LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Access to text file line by line

Solved!
Go to solution

Hello,

I need to get access to data saved in a TXT. I was trying to achieve that by doing something like this:

readfromfile.png

However, I only get the first line.

In this VI the indicator is just to see if the lines are being picked correctly from the file. When I get the right procedure, this strings will be passed to another processing.

Can anyone help me with any idea how to do this.

Thank you.

Duarte

 

0 Kudos
Message 1 of 8
(7,636 Views)
Solution
Accepted by topic author DuarteCota

If you right-click on the Read Text File, you will see an option to "Read Lines".  If you enable that and set the number to read to -1, you get an array of strings, one element per line.  If you leave the number of lines to read unwired, it will read 1 line at a time.

 

If this is not your problem, perhaps I am misunderstanding your issue.  From what I see in your code, it should read the entire file on the first iteration.  After that you should be getting an empty output and an error (end of file encountered).  Perhaps your indicator is only showing 1 line (ie you need to make it bigger height-wise).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 8
(7,625 Views)
Solution
Accepted by topic author DuarteCota

I suspect the problem is the size of your indicator.  If you run this VI, it should read the entire file but the remaining lines aren't displayed because they're below the visible part of the indicator.

 

But, let's take a look at this VI.  You open a file that's absolutely named (I'd strongly suggest something relative), send it into a while loop where you've told it to read the entire file (without right-clicking read lines so it's looking for characters), and display the entire file to the indicator.  Then, you wait until the 500ms wait completes and try to read again.  Although, you haven't reopened the file so you're still at the same cursor point you were before.  As a result, you open the file, find the EOF, and display nothing.

 

You don't really want most of what you currently have.  Here's now to fix what you do have. 

 

1) Right-click the border of the while loop and choose "Remove While Loop"  You don't need it.  You're not looping.

2) Delete the text indicator

3) Hit ctrl+b to clear the broken wire

4) Right-click the read file and place a check next to "read lines"  This will change the output to an array

5) Right-click the output and choose Create->Indicator

 

Hit run.  You'll see each line from that text file is now an element of the array.

 

As your goal is to pass these strings to another area for processing, you can use this array of strings to "auto-index" a for loop later and run the for loop exactly as many times as you have strings.

 

 

0 Kudos
Message 3 of 8
(7,613 Views)

Thank you all very much!

With your tips now I have an array of strings! And yes, I do not need a loop to do this:

 

readfromfile.png

 

So now how do I get each string at a time? I was thinking about using a for loop or a while with some kind of indexing. Do you know the function to get each string?

Thank you!

Duarte

 

0 Kudos
Message 4 of 8
(7,610 Views)
Solution
Accepted by topic author DuarteCota

As a tip, I'd check out: http://www.ni.com/white-paper/5241/en/  This is a course to learn some LabVIEW in 6 hours.

 

If you create a for loop and wire an array in as an input, it will create what's called an "auto-indexing tunnel"  This tunnel will take an element, in order, from the array to use in each iteration of the for loop.  When the array is empty, it'll stop processing.  It handles all of the indexing for you.  Topics like these are what's covered in the provided link.

0 Kudos
Message 5 of 8
(7,595 Views)

Hello again and thank you.

I did it. It works using a for loop or a while loop.

Now can you help me stoping the VI when the end of the file is reached?

Is there any EOF control that I can use?

Duarte

0 Kudos
Message 6 of 8
(7,574 Views)
Solution
Accepted by topic author DuarteCota

Why would you have any issues stopping that?  Did you use the while loop?  If so, you'll have to create some logic to continue using the while loop.  Essentially, read the auto-index tunnel.  If "Equal?" to "Empty String Constant" stop the loop.

 

Or, just use the for loop.  The first VI will only create as many elements as you had rows.  This takes care of the EOF for creation of inputs.  The for loop runs exactly as many times as you have elements (leave the N terminal unwired and just wire the array in).  When it's completed every element, the for loop completes and the application is finished.

 

Where are you running into logic to stop the VI?  Or, what are you doing to keep the VI running?

0 Kudos
Message 7 of 8
(7,567 Views)

Closed!

Thank you very very much.

Best.

Duarte

0 Kudos
Message 8 of 8
(7,548 Views)