Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing a line from a text file?...

Status
Not open for further replies.

gareh

Programmer
Apr 27, 2003
4
GB
Hi.
I'm trying to remove a line from a file that holds user profiles. The file is as follows:
Code:
admin:god:group1:ksh:1
user1:pass1:group1:bash:1
user2:pass1:group2:bash:0
user3:pass1:group3:bash:1
user4:pass1:group4:bash:0
So I have a form which can select which username to delete, but I don't know how to actually delete a specific line from the text file. Can anyone point me in the right direction?

One idea I have was to create a 2nd file, reading each line except that of the one I wish to remove, then rename the new file to the original filename. There must be an easier way to do this?!

Many Thanks
 
hi gareh

Unless you use a database you have to do it exactly the way you don't want to - read through first file, write second file with everything except record you don't want, delete first file, rename second file to first file.

If you're dealing with "a lot" of records it will be worth working with a database.

In this context the phrase "a lot" means "more records than I can process in a reasonable time"

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Cheers. Got it working my way. will look into using a database. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top