Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Read an .ini file and then launch commands from a console application ?
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 :
What 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 Hwaddr
I don't know if qt already has function like this or of it would be possible in cpp.
For running external apps you can use QProcess.
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.
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().
wowy last edited by wowy
Ok, thank you i will take a look at this ! (learning every day)
EDIT : I tried :
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.