MViControl Posted October 9, 2009 Report Share Posted October 9, 2009 Hi all, I have a .txt file which includes several lines, and I want to delete certain line(e.g. line 5) in the file, I found Labview have no function for this, so I would like to operate like following steps: 1. open file in read/write mode; 2. read all lines to a array; 3. delete the line in the array; 4.write updated array to the file(replace all the old contents) do you have any other way to operate this easier? Quote Link to comment
ShaunR Posted October 9, 2009 Report Share Posted October 9, 2009 Hi all, I have a .txt file which includes several lines, and I want to delete certain line(e.g. line 5) in the file, I found Labview have no function for this, so I would like to operate like following steps: 1. open file in read/write mode; 2. read all lines to a array; 3. delete the line in the array; 4.write updated array to the file(replace all the old contents) do you have any other way to operate this easier? 1. Read from spreadsheet file. 2. Delete array element. 3. Write To Spreadsheet file. Either way its only a 30 second vi. How easy do you want it? Quote Link to comment
MViControl Posted October 9, 2009 Author Report Share Posted October 9, 2009 (edited) 1. Read from spreadsheet file. 2. Delete array element. 3. Write To Spreadsheet file. Either way its only a 30 second vi. How easy do you want it? Thanks, just want to know how people handle this. B.T.W how you handle the replacement of the file contents(if you just write txt to file, it will attached from the last point of orignal contents, if you write from the start point of the file, there will be a orignal line left in the last line of the new file), I delet the orignal file and create a new file to avoid this. Edited October 9, 2009 by Techie Quote Link to comment
Cat Posted October 9, 2009 Report Share Posted October 9, 2009 Thanks, just want to know how people handle this. B.T.W how you handle the replacement of the file contents(if you just write txt to file, it will attached from the last point of orignal contents, if you write from the start point of the file, there will be a orignal line left in the last line of the new file), I delet the orignal file and create a new file to avoid this. I do the same thing Shaun suggested. It would be really nice if LV had a "Replace a Line in a String" function. Even better yet if it had a "replace last line in string" option. I've written my own vi for the latter. Sometimes it works and sometimes it doesn't. I don't delete the original file, but use "Create or replace" when writing the file contents back. Quote Link to comment
vugie Posted October 9, 2009 Report Share Posted October 9, 2009 I don't want to split a hair, but just to mention that as these methods require to load whole file to memory they'll work well for files not exceeding let's say 100-200 MB. Quote Link to comment
Cat Posted October 9, 2009 Report Share Posted October 9, 2009 I don't want to split a hair, but just to mention that as these methods require to load whole file to memory they'll work well for files not exceeding let's say 100-200 MB. Considering the 4 copies in memory that might happen, I'd be lucky to get that 200MB... You are right; this method is definitely for handling small files. The vast amount of time I use it for editing configuration files. Quote Link to comment
Shaun Hayward Posted October 9, 2009 Report Share Posted October 9, 2009 When dealing with larger files, you could do the same basic method but just reading in a chunk of lines at a time - e.g: Read, say. 100 lines Check to see if required line is in the chunk, and make change if necessary Write that chunk do new file See if any lines remain Also, if the line is always a fixed number of characters you could also use the Set File Position VI to move to the specific location in the file and then just overwrite that portion. Quote Link to comment
ShaunR Posted October 9, 2009 Report Share Posted October 9, 2009 Considering the 4 copies in memory that might happen, I'd be lucky to get that 200MB... Still reeling from that one eh? Quote Link to comment
Cat Posted October 9, 2009 Report Share Posted October 9, 2009 Still reeling from that one eh? I've written and deleted a few rants... Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.