Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,679 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,205 people online right now. Registration is fast and FREE... Join Now!




Simple program to edit a file

 
Reply to this topicStart new topic

Simple program to edit a file

Captain M
post 11 Jul, 2007 - 12:14 PM
Post #1


D.I.C Head

**
Joined: 21 Jan, 2007
Posts: 95


My Contributions


I wrote a simple program to edit a text file on my computer. I had a problem that every time I started up my computer, I would have to manually change the order of my dns servers. It got really annoying, and I didn't know how to fix it in the os, so I just wrote a simple little program to do it for me. The problem is, the file is only writeable by root, causing me to have to type my password in. This causes the computer to not be able to run the program by itself. How can I make the program work? Here is my code for referance:
CODE
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main (){
    system("sudo cp /home/family/resolv.conf /etc");
     return 0;
}


And here is the file it copies:
CODE
# generated by NetworkManager, do not edit!

search domain.actdsltmp


nameserver 205.171.3.65
nameserver 192.168.0.1
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 13 Jul, 2007 - 07:21 AM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


I thought that resolv.conf existed in /etc/?

A workaround you could make a cron job to run your script.

Check your sudo settings to remove the password option.

CODE

sudo gedit /etc/sudoers


The file is probably empty. If so, add this line :
CODE

username ALL=(ALL) ALL


I'm not familiar with sudo, so if this information is incorrect, somene post the correct solution.

This post has been edited by no2pencil: 13 Jul, 2007 - 07:27 AM
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 13 Jul, 2007 - 08:05 AM
Post #3


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


A better way to do it would be to set it up in a bash shell script, and add it to the default runlevel, so it runs on every boot.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 13 Jul, 2007 - 08:27 AM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(MorphiusFaydal @ 13 Jul, 2007 - 09:05 AM) *

A better way to do it would be to set it up in a bash shell script, and add it to the default runlevel, so it runs on every boot.

You mean like put it in /etc/rc.d/rc.local ? That runs after gpm! Or under one of the rc.S* run levels, yeah. I can see that working, good idea.
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 13 Jul, 2007 - 10:20 AM
Post #5


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(no2pencil @ 13 Jul, 2007 - 09:27 AM) *

QUOTE(MorphiusFaydal @ 13 Jul, 2007 - 09:05 AM) *

A better way to do it would be to set it up in a bash shell script, and add it to the default runlevel, so it runs on every boot.

You mean like put it in /etc/rc.d/rc.local ? That runs after gpm! Or under one of the rc.S* run levels, yeah. I can see that working, good idea.


I think the bigger problem is why does he need to rewrite /etc/resolv.conf on every boot? Why not set dhcpcd or dhclient to not overwrite it everytime it runs?
User is offlineProfile CardPM

Go to the top of the page

Captain M
post 16 Jul, 2007 - 07:53 AM
Post #6


D.I.C Head

**
Joined: 21 Jan, 2007
Posts: 95


My Contributions


Sorry, I've been gone. I have no clue why I have to edit the file every time. I'm going to answer your questions in the order they were asked. no2pencil, resolv.conf does exist in /etc, I just have another one sitting in my home folder that copies over the one in /etc. If you notice, the last thing my code says is /etc, as that tells it where to copy to. You probably already knew this and just missed the last part of my code. And as for no2pencil's solution, would that not just make it so you never have to type a password to sudo? That's dangerous and something I don't want. I'm pretty sure somewhere in this ubuntu system ages ago I found list of programs that run on startup, and just added mine to it. I can't seem to find it now though. I don't really understand what you're saying about editing that other file, could you explain it more?
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 16 Jul, 2007 - 11:49 AM
Post #7


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(Captain M @ 16 Jul, 2007 - 08:53 AM) *

Sorry, I've been gone. I have no clue why I have to edit the file every time. I'm going to answer your questions in the order they were asked. no2pencil, resolv.conf does exist in /etc, I just have another one sitting in my home folder that copies over the one in /etc. If you notice, the last thing my code says is /etc, as that tells it where to copy to. You probably already knew this and just missed the last part of my code. And as for no2pencil's solution, would that not just make it so you never have to type a password to sudo? That's dangerous and something I don't want. I'm pretty sure somewhere in this ubuntu system ages ago I found list of programs that run on startup, and just added mine to it. I can't seem to find it now though. I don't really understand what you're saying about editing that other file, could you explain it more?


When your system boots, it runs a collection of scripts to get everything that is needed to run your computer, running. There is one specific script that you can add your own stuff to. So, you would create a small bash shell script like such:

CODE
#!/bin/bash
cp /root/resolv.conf /etc/resolv.conf
echo "resolv.conf copied..."


And save as /root/myResolv.sh or something like that. chmod +x myResolv.sh to make it executable, and add /root/myResolv.sh to local.start. (local.start is the Gentoo-version of the custom init scripts... I don't know where Ubuntu keeps theirs.

Although, if I recall correctly, and someone check me on this, you would actually name it something like "77-myResolv" and put it in /etc/rc.d/rc.3, still make it executable, and then it'll run on it's own.

Of course, in that script, you'll have to modify where that resolv.conf is. I'd recommend putting it in /root, just so if you screw something up as user, and your ~ gets blown away, you don't get screwed next time you boot.
User is offlineProfile CardPM

Go to the top of the page

Captain M
post 17 Jul, 2007 - 05:58 PM
Post #8


D.I.C Head

**
Joined: 21 Jan, 2007
Posts: 95


My Contributions


Hmm I don't know. That sort of sounds risky. Is it?
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 17 Jul, 2007 - 09:09 PM
Post #9


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(Captain M @ 17 Jul, 2007 - 06:58 PM) *

Hmm I don't know. That sort of sounds risky. Is it?


It's doing exactly the same thing that the program you want to write is doing.

Either way, you're calling something at boot time to copy one file from one place to another. The program you want to write, if you want it to run automagically, will have to be called from a script in /etc/rc.d/rc.* anyway. So why bother writing a program to call 'cp' that's getting called from a script that can call cp itself? Cut out the middleman.

Scripting is all about the pursuit of laziness; getting the most done with as little work as possible.

More on the riskiness: That's why the rc-scripts are where they are. So you can add your own. Check the Ubuntu-docs for direction on exactly where to put it. #ubuntu on irc.freenode.net would also be a great place to ask if they can help you. No guarantees on the snideness of any given response, though.
User is offlineProfile CardPM

Go to the top of the page

Captain M
post 19 Jul, 2007 - 09:13 AM
Post #10


D.I.C Head

**
Joined: 21 Jan, 2007
Posts: 95


My Contributions


Ok, thanks!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month