Read an .ini file and then launch commands from a console application ?
-
Hi,
I am trying to make a qt console application which could read a .ini file and then run commands with the data from this file.My ini look like this :
[IP]
MAC=61:55:60:62:4e:bc
ADDRESS=10.2.29.255
NETMASK=255.255.0.0
GATEWAY=10.2.1.1
DNS=10.2.2.110
DHCP=0What i would like to know how to too would be how to read the data in this file and how i could use them to launch command like :
ifconfig lo 127.0.0.1 netmask 255.0.0.0
ifconfig eth0 10.2.29.255 netmask 255.255.0.0
route add default gw 10.2.1.1
ifconfig eth0 down
ifconfig eth0 hw ether 61:55:60:62:4e:bc
ifconfig eth0 up
ifconfig eth0 |grep HwaddrI don't know if qt already has function like this or of it would be possible in cpp.
-
-
@Chris-Kawa
Ok, i will take a look at QSettings and value().QProcess is only for external apps or shell command ?
-
QProcess won't run shell commands, but you can run the shell itself and pass a script or commands to it as parameters.
-
@Chris-Kawa
Ok.
Well i don't think i will be able to use QProcess because the script will not be able to use the data from the ini read with Qsettings if i understand. -
It's very well possible. In pseudo code:
QSettings s("path/to/settings/file.ini", QSettings::IniFormat); QString someValue = s.value("section/optionName").toString(); QString someOtherValue = s.value("section/anotherOptionName").toString(); QString commandToRun = QString("some command with param %1 and %2").arg(someValue, someOtherValue); QProcess p; p.start("sh", QStringList() << "-c" << commandToRun); p.waitForFinished();
Another way is to start "sh" without params and then write the commands to its input stream using QProcess::write().
-
Ok, thank you i will take a look at this ! (learning every day)
EDIT : I tried :
#include <QCoreApplication>
#include <QSettings>
#include <QProcess>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);//QSettings s("/mnt/SD/CHIP.INI", QSettings::IniFormat); //QString someValue = s.value("IP/ADDRESS").toString(); QString commandToRun = QString("printf hello"); QProcess p; p.start("sh", QStringList() << "-c" << commandToRun); p.waitForFinished(); return a.exec();
}
I the application don't "printf hello" and don't close after this.
-
It probably does print it just not where you think it does. The output of the external process is not redirected automatically to the callers shell window. If you want to see what the process wrote call readAllStandardOutput to read its standard output. You can then output it in your app if you want to.
The app doesn't exit because you started an event loop (
a.exec();
) and never exited it. If you want to exit the app at this point just don't call exec.