LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Replace some texts in a text file for selected set of lines

Hallo Everybody,

Dear Jim,

 

I like your 20th message much because I have the same problem.

If I do not have the “read lines from file.vi”, how do I have to adapt DFGray’s program (see attachment in the 19th post) so that it does not truncate my file (see my explanation in the attchement)?

 

I would be happy to have some suggestions, even if this post is a bit older.

0 Kudos
Message 21 of 28
(879 Views)

Everybody have the "Read Lines Form File.vi", it is called the Read From Text File.vi. When you drop it on the block diagram right click on it and select Read Lines.

 

Now you have to understand DFGray example. It use the Set File Position.vi and the offset is set to 3 bytes (that is 3 characters for a text file, so it will start to read after Lin of Line 1, that is e 1 for the rest of the line) and then from this offset you will read 2 lines. That's why you get:

 

e 1

Line 2 (not Line 3)

 

In you case you don't need to set an offset and you want to read all the lines so set the count input of the Read From Text file.vi to -1. I suggst you read the detailed help for this function.

 

Ben64

Message 22 of 28
(866 Views)

Hi Ben64,

 

Thanks for your reply and explanations. I see that I made a mistake in my previous post. I apologize for this. And perhaps I asked my question in a bad way.

Let me first mention the reason why I asked this question: I have some big files (several 100 MB) which I have to analyze with LabVIEW. If I could split my file in different chunks, I could handle them better and more quickly.

 

So I tried to find a solution for this problem and found this post.

Sure, I know the “Read From Text File.vi”, but I thought form the 20th message that there is/was a special vi (e.g. the no longer supported "Read Lines Form File.vi") with which you can start reading files from a special line (!) (and not only with an offset in bytes as done by the “Set File Position.vi”). But I fear that this was mentioned not correctly in this post.

 

Well, for my purpose I have to use an offset and I do not want to read all the lines (I want to my program to use the e.g. first 100 lines, and then lines 101 to 200 and so on from my file). To explain better, what I want, I attach a corrected version of my explanation.txt.

Sure, I could do this with some array operations after reading in the whole file. But that is the problem: the whole file is too big to read by my computer with LabVIEW. So, I thought it would be a good idea to read in just chunks of the file with “Set File Position.vi” and with “Read From Text File.vi”. But to use “Set File Position.vi” I have to know the offset in bytes and I do not have an idea how I could find out how many bytes my first 100 lines have.

To read in just a special number of lines with the “Read From Text File.vi” I could chose Read Lines from the menu by rightclick and use then “count”. But I think that this is slowly (as you mention in your 19th post). It would be better to know how many bytes line 101 to 200 have and to select them with this number via “count”. But one again, I also do not know how I can find out this.

 

Well, I hope, I have explained my problem a bit better now.

I would be very happy if you or somebody else could tell me now, how I can read in a file in chunks (without truncating it as the example program does it at the moment).

0 Kudos
Message 23 of 28
(857 Views)

partial81, your biggest problem is that you are trying to deal with a file in terms of lines, but, fundamentally, files are accessed in terms of bytes/characters.  If your file always has the same number of characters per line, your job is much easier, but one cannot assume that and maintain robust code.  So, the easiest way to get an arbitrary series of lines is to read in the file a chunk at a time and look for end-of-line characters in the chunk until you have found the bounds of the data you are interested in.  At that point, you can then save off this chunk (it should be in memory already) and do what you need to with it.  An algorithm which will always work to get lines n to m is as follows.

 

  1. Read in 65,000 characters using the read primitive.  65,000 is used for the chunk size because it gives about the most efficient disk transfer speed for Windows systems.  You may want to tune this for Linux and OS X systems.
  2. Search the chunk for end-of-line (EOL) characters.  If you find the nth EOL, start storing text from just after it to your final buffer.
  3. If you do not find the nth EOL, continue reading in more chunks and processing them until you do find it.  If your EOL is two or more characters, don't forget that it can occur on a chunk boundary.
  4. After finding the nth line, look for the mth line in the same fashion.  However, you are not appending text to your final buffer from each chunk.  When you do find the final EOL, only append text up to that point, not the entire chunk.

This approach can cause you memory issues if you are dealing with huge amounts of data.  In that case, you may want to either record the line locations in the file or process the data as it is coming in.

 

Good luck.

0 Kudos
Message 24 of 28
(834 Views)

Hello,

 

I have same issue like above.I want to read the first charater of the last line.I am also appending the string each time in one .txt file.For more clear discription i have attached on subvi and text file.

Download All
0 Kudos
Message 25 of 28
(782 Views)

I would just read the text file by lines (right click on the Read Text File and select Read Lines).  If the file is fairly small in size, I would read the entire file (set count to -1) and get the final line.  From there you can figure out which characters you want.


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
0 Kudos
Message 26 of 28
(771 Views)

Hello,

 But if you run my vi ou can see i am appending every time in text file.So i want to read every time the first character of the last line.In one text file no of lines will increase whenever i run this loop.

0 Kudos
Message 27 of 28
(764 Views)

Why can't you just save the needed characters in a shift register or global variable when you write to the file.  Then you can simply read the values from that instead of the file.


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
0 Kudos
Message 28 of 28
(761 Views)