LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-26-2005, 05:14 PM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Delete line from flat text file in C


Hi all,

I am writing a program that is going to be involved (among other things) in binding and unbinding IP addresses from the server's interface.

To bind the IPs, it adds IP:netmask, e.g. "200.200.200.200:255.255.255.0", to the end of a plain text file, and then calls a script which makes the necessary additions elsewhere (this is a server running cPanel, if anyone is familiar with that, not that it makes any difference). So the text file might look like this:

200.200.200.13:255.255.255.0
142.203.65.123:255.255.255.0
65.46.57.0:255.255.255.255.0

and so on. OK, easy enough.

To unbind the IP address, it needs to do the opposite: Remove that IP's entry from the text file, and then call the same script. My question is, what approach would you take? All I can think to do is to open the first file for read, read one line at a time, and if the line does not match the IP:netmask to be unbound, it is copied to a temp file. If the line does match, it is not copied. Then the file is closed and deleted, the temp file renamed to the first file's name, and the cPanel script called.

Can anyone think of a better approach? This sounds kind of wasteful, although I am sure that the resources consumed would not be a problem.

TIA.
 
Old 01-26-2005, 05:47 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
To remove:
Code:
sed -i.backup '/200\.200\.200\.13:255\.255\.255\.0/d' binding_text_file.txt
To add:
Code:
echo "200.200.200.13:255.255.255.0" >> binding_text_file.txt
EDIT: sorry, you would need to enclose those in equivalent "system" calls, or something from the exec family...

Last edited by Dark_Helmet; 01-26-2005 at 05:50 PM.
 
Old 01-26-2005, 05:57 PM   #3
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
The approach you suggest is exactly the way to go. This is how file access is done.

What Dark_Helmet has suggested besides providing a load of overhead, these commands would actually do exactly what you'd be doing anyway.

Last edited by leonscape; 01-26-2005 at 06:00 PM.
 
Old 01-26-2005, 05:58 PM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi, thanks for the reply! That seems like it would be more appropriate in a shell script than in a C program. I'm not arguing with you; would it be more efficient for the program to fork a child and then call exec(sed) than my scheme?

Thanks again!
 
Old 01-26-2005, 06:04 PM   #5
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi leonscape, you must have posted while I was writing my previous post, which was for Dark_Helmet.

I will go that route then, it just seemed like there ought to be a different and more efficient way to do this, but I couldn't come up with anything.

Thanks again to both of you!
 
Old 01-26-2005, 06:06 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
When I first read the original post, I thought this:
Quote:
All I can think to do is to open the first file for read, read one line at a time, and if the line does not match the IP:netmask to be unbound, it is copied to a temp file. If the line does match, it is not copied. Then the file is closed and deleted
meant you were doing the editing by hand.

A misunderstanding on my part, and yes, leonscape is right, using the method I described would add a bit of overhead. That type of solution is better placed in "quick and dirty" programs where you just want something to work and get it to work quickly. At least, that's the philosophy where I work. Sorry if I led you down an inappropriate path.
 
Old 01-26-2005, 06:16 PM   #7
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi again,

At the present time we actually are doing the editing by hand. This program I am writing will, as a sideline, take that over.

But actually, until it's done, I will use the info you provided in your first post. I'm actually surprised I never thought of the first one myself, though I never would have come up with second one.

And it was my fault for not mentioning in my post that I was writing a C program. So no worries.

Thanks once again for your input. This place rocks!

Edit: Actually, I actually wonder if I could actually use the word "actually" more than I actually have in this post.

Last edited by zaichik; 01-26-2005 at 06:19 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
delete chunks or regions from text file - help K3vin Linux - Software 3 09-26-2005 02:57 AM
How do I delete a line in a file with fstream (C++)? CrazyHoboMan Programming 3 08-18-2005 02:06 AM
locate and delete a line from a file eilison Programming 3 05-17-2005 06:07 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 03:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration