LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get read an entire line from a .txt file?

Hello! I'm using LV8.6.1. I need to read a line (row) from a text file (.txt) but only that line. 

The txt file contains some product codes and a value (like a small database). I insert the code from a barcode scanner and I want to read the value associated to this code.

 

For example:

 

Text file:

code           value 

ZA-156336 20

ZA-194066 50

ZA-123987 70

ZA-567153 30

...etc.

 

Codebar scanner string:

 

ZA-194066

 

I want to read the code AND the value associated to this code. So, the code should be a part of the line from my text file and, if match, it have to return the whole text line (row), nomatter where it is located in the text file.

 

I have used Scan From File, but I only can obtain the first line (row).

 

Is there anyone can help me to understand and to explain what to do to get what I need? If possible, please post here an example. Thank you.

0 Kudos
Message 1 of 10
(5,606 Views)

hello siconi,

  Actually you can do this using read from textfile function.Before using this function you right click on the function and check the readlines option and make sure to uncheck the convert Eol.So now wire the count terminal to read the number of lines in the file and those lines will be returned as an array.Now use some string comparison functions and get the result.

 

Please let me know if any further queries.

 

Thanks and regards,

srikrishnaNF

Regards,
Srikrishna


0 Kudos
Message 2 of 10
(5,602 Views)

Thanks for trying. I have used read from textfile function too and it was the same: I can read only the first row.

I guess I need to write again:

 

I scan a barcode containing the type, the quantity and the destination information about that product. I can filter in order to obtain only the type (I will call this product code). OK, so now I have only the product code.

I have created a text file (.txt) which contain a small database about each kind of product: product code and an associated value that tells me the weight of the product. In order to be more explicit, the product is a wire and the value associated to the code is the weight of one single wire. when I scan the barcode, I want to be able to read in my .txt file only the row that contains the specific code and its weight. The weight it's not included in the barcode and I need it to determinate the total weight of the bunch from which I scanned the barcode.

So, I have to read the whole row when I scan a code, no matter where it is located into the text file.

 

Again: I have used the Scan From File and Read From File but with the same result.

Can you figure out another solution?

If you need, I can give you my email address to attach some demo files.

Best Regards. 

0 Kudos
Message 3 of 10
(5,566 Views)

How big is your text file?

 

If it is not too large, then the best solution is to read in the text file and break up the line into different parts that you could store in a 2-D string array.  Then you can sort the array as needed.

 

Now when you need to find the item of interest, you just search the 2-D array on one column and use that to pick out the other column that contains the weight data.  That way you only need to read the text file once.  If you have the text file presorted, then you wouldn't have to sort it, and it might speed the searching of the array.

0 Kudos
Message 4 of 10
(5,563 Views)

You have some serious misconceptions about reading "lines" from files. A file is just one long string and trying to find a "line" is a matter of reading starting at a "newline" character until the next "newline" is found, comparing the following characters (i.e. the beginning of the line) with your template, then continuing until a match is found. All matching operations needs to occur in memory.  It is not efficient to go back to the disk for each character because that's extremely slow compared to anything you can do in RAM. You need to limit disk IO operations.

 

How big is the file? Unless it is hundreds of megabytes, it is always better to read the entire file into memory and do the matching there as plain string operations. For example you could use "search string for tokens", then read the number beginning at "offset past match" location. You say it is a small database, meaning the file is not that big.

 

If your database is huge, you can use more advanced techniques, e.g. splitting the database into multiple files.

 

Can you attach a typcal datafile?

0 Kudos
Message 5 of 10
(5,552 Views)

The file is not so big as you might expect. It has less than a 1MB and contains about 1000 lines / rows (call them as you wish) with 30 alphanumeric characters. So it's not a big deal, but I am not able to handle it as I need.

 

I have to match a string (containing the product code) to the product code inside of file and, if really matched, it have to return the whole line with the matched code and the rest of characters from that line.

For example:

 

ZA-156336 is the code of the product I receive from barcode scanner

 

ZA-194066 20

ZA-194067 50

ZA-156336 40

etc.

 

If the code from scanner match with the 3rd line, it has tu return the whole line "ZA-156336 40" and ONLY this line, nothing more or less.

 

The file contains about 1000 lines (maybe less, I don't know yet; Does not depend on me, but the owner of aplication).

0 Kudos
Message 6 of 10
(5,541 Views)

Since you said that is was a "small" database file, the Ravens Fan's method should work for you.

 

Attached is an example of that method. 

 

The example assumes that the text in the file is delimited with spaces (I'm guessing on this from your first post). It would be better if the text file was delimited with tabs (in that case just change the delimiter on the Spreadsheet String to Array function). For example, if the header has 11 spaces separating the words "code" and "value", 12 columns would be created in the array. Use tabs, and you only get two.

 

The example takes an array of codes as input. From your posting I thought that might be useful, but if not, you could remove the for loop or use a build array function to make your input code an array of 1 item. It also checks to see that the code was found and that there was a value found for that code (OK, I was bored :)).

 

Oh, I just noticed you added a post that said you just wanted the line in the file, so I added a version that does that (read file line.vi).

 

steve

----------------------------------------------------------------------------------------------------------------
Founding (and only) member of AUITA - the Anti UI Thread Association.
----------------------------------------------------------------------------------------------------------------
Download All
0 Kudos
Message 7 of 10
(5,533 Views)

one moment...

Message Edited by altenbach on 11-19-2009 12:45 AM
0 Kudos
Message 8 of 10
(5,532 Views)

No need for all these manipulations (arrays, loops, etc.) 

 

Try the following:

 

(return the matched string including everything up to the next linefeed)
Message Edited by altenbach on 11-19-2009 12:59 AM
Download All
Message 9 of 10
(5,525 Views)

> No need for all these manipulations (arrays, loops, etc.) 

 

 

very true

----------------------------------------------------------------------------------------------------------------
Founding (and only) member of AUITA - the Anti UI Thread Association.
----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 10 of 10
(5,515 Views)