LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Delete line from flat text file in C (https://www.linuxquestions.org/questions/programming-9/delete-line-from-flat-text-file-in-c-282615/)

zaichik 01-26-2005 05:14 PM

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.

Dark_Helmet 01-26-2005 05:47 PM

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...

leonscape 01-26-2005 05:57 PM

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.

zaichik 01-26-2005 05:58 PM

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!

zaichik 01-26-2005 06:04 PM

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!

Dark_Helmet 01-26-2005 06:06 PM

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.

zaichik 01-26-2005 06:16 PM

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.


All times are GMT -5. The time now is 08:27 PM.