calling unix commands from a c/c++ program

Hello members,

I am familiar with codin in c c++. However, since I have not worked much on it, I would be grateful if someone could throw light as to if this is possible from a C/c++ program.

  1. Can we call unix commands like ifconfig from a c++ program to modify an ip address. The modification will of ipaddress is on a local network, so question of messing up with ip address should not be there.

  2. can we run script files, from c++.

If the above can be done, can anyone, please point me in the right direction as to how, it is done, or if to any web resource if available.

Thanks :question:

You can use the spawn() family to do that.

system() also isn’t bad idea.

or popen()

Hell All,

Thanks for your help regarding my query. Now, I have another question.
I have set up a connection between a windows machine and a QNX 6.2 machine. From the Windows machine, through a C++ program I remotely try to open up a file in QNX machine, and make some changes in the file. It works fine.

However, when I do the same thing, of trying to edit remotely from a windows machine, a file running in a QNX ( using compact flash). It does not edit the file. The QNX has the file I want to edit, it also has all the read write permissions. So why does it not reflect the changes.

Do I need to write my C++ program in a different way for doing functions in QNX running from compact flash. Right now I am using fstream class for
file handling. As mentioned earlier it works fine when running in a full version of QNX, but not in the version running from Compact flash.

Any guidance regarding the same would be greatly appreciated.


How did you setup the filesystem in the flash?

Can you edit the file via an editor for example?

Hello Mario,

Yes, I can edit my file when I run QNX through Compact Flash. I can use the vi editor to edit the file.

For setting up the file system i have done nothing specific. when i build my CF image, I do
fdisk /dev/hd1
and in there i have set aside hd1t79 for boot.

when the QNX loads from the CF image. I have the following drives
/hd /proc /tmp /usr

My file that i want to edit gets stored in the /hd/
A copy of it is also stored in /proc/boot/

However, the one I want to edit is And, now I can edit it. I was referring to the wrong file earlier.

I have another question regarding the same. How can i make the change in the file persistent, so that the change gets refleted everytime I boot the system fom CF. currently it goes and restores the default contents in the file.

thanks for your time.


If /hd/ is a link to /proc/boot/ or is copied by the boot image.

I have seen some flash device that had their write buffer enabled and would lose their data on reboot. This was on QNX4 for and we had to get the manufacturer to change their device to have this write buffer disabled ( i guess it was relying on the OS to disable it)

Hello Mario,

I am using QNX 6.2. /hd/ is copied by the boot image. Anyway, I have figured a workaround, regarding the issue. I am not editing the directly from my program. Hence, its not an issue.

I am getting another error, I am also posting it as a separate thread. but if you have any idea, regarding the issue, please do guide:

Now, when i am running my QNX 6.2 from the CF image.
I have a drive /hd. I store my files that i need to manipulate/edit here. By default I have three files here in /hd. If I copy an existing file for example and rename it It creates the new file
When i do ls: it shows yyyyyyyyyyyyyyyyyyyyyy (with two dots on top of each y).
when i do ls -l : it says
ls: Corrupted file system detected (./yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy) with two dots on top of each y.

I am not able to figure out, why does it say that the file system is corupted if I add an extra file. The new files works fine so does the old. This corruption thing does not happen if I edit my existing default files, its only when i add a new file to the directory either via move, cp or so on, then it says the file system is corrupted.

Thanks a lot for your help/


Hi All,

I am also facing a similiar sort of problem where accidentally a single bit of my file gets inverted, i.e. when i try to read file and write it to another file, i get a bit inverted randomly anywhere in the written file.

This problem is very occassional but whenever it comes , it destructs my whole program, as checksum of the file fails…

I am using simple fread() and fwrite() calls…
Any idea regarding the possible root cause ?


Most probably a hardware problem. cable, bad hd, ide cable to long, bad ram, bad power supply, etc