Jump to content

Delete a line in a file


Recommended Posts

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?

Link to comment

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?

Link to comment

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 by Techie
Link to comment

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.

Link to comment

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.

Link to comment

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.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.