Search and remove a line in a text file
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sky_blu3
Posted On:   Monday, January 8, 2007 10:01 PM

Search and remove a line in a text file Hi, I would like to know if there is any proper way which I can search and remove a line in a text file, like : Content in Text File : ============================= < Andy Andrew John Patrick Sam Sky . . . ============================= - Open file, search and remove the line with value "John". - Once "John" is found, remove that particular line and save immediately without continue to read the rest of the file. Any advice will be appreciated. Thanks!    More>>
Search and remove a line in a text file
			
Hi, I would like to know if there is any proper way which I can search and remove a line in a text file, like :

Content in Text File :
============================= <
Andy
Andrew
John
Patrick
Sam
Sky
.
.
.
=============================

- Open file, search and remove the line with value "John".
- Once "John" is found, remove that particular line and save immediately without continue to read the rest of the file.

Any advice will be appreciated. Thanks!
   <<Less

Re: Search and remove a line in a text file

Posted By:   Robert_Lybarger  
Posted On:   Monday, January 8, 2007 10:40 PM

You can't really just remove a subset of bytes from a file the way (that I understand) you are thinking: you'll have to write a new file with the material before and after the offending line, and to get the "after" material, you have to read it somehow. In other words, you can't just say "without continuing to read the rest of the file", as it doesn't make any sense. This is with respect to any general programming language.

If you are worried about performance in general, what you could probably do is change over from line-level file reads to byte-block level file reads once you find the line you want (or with even better code -- and depending on the nature of your real problem -- you could work entirely in byte-block level reads ... though you'd have to account for the possibility of the byte block only partially capturing the line you are looking for).

About | Sitemap | Contact