Results 1 to 5 of 5
Hi All,
I have developed an application in cpp , now I have to use few linux commands in cpp application and have to use the results of those linux ...
- 06-24-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 3
How to embedd Linux commands in C++ code and how to use the output of those commands
Hi All,
I have developed an application in cpp , now I have to use few linux commands in cpp application and have to use the results of those linux commands to manipulate my application.
Please help me how can I embedd and use its result of linux commands in cpp applications.
some of the linux commands are as follows :
- iwconfig
- iwlist
- iwspy
- iwpriv
- ifrename
Please suggest how to use it in cpp.
Regards,
Kamakshi
- 06-24-2009 #2
From C program , you can always use function like system() to execute a command. Check whether C++ has system() function or similar functions.
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 06-24-2009 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
As Lakshmipathi said, you can use the system() function to execute a shell script. If you want to use the output from them to affect futher processing of your application then you will need to redirect the output of these commands to a file and read the file after the command completes.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-24-2009 #4
First off, right after I post this, I will move this thread into the programming forum.
Secondly, it depends on what exactly you are trying to accomplish.
If all that you want to do is execute a program, then system() is usually the way to go. system() takes a commandline, and then runs it. For instance:
This would run the "iwconfig" command, and would print out whatever it would output.Code:system("iwconfig");
However, if you want to use the output of this command in your own program, then system() isn't very good. Instead, you want a function that I don't believe C++ has, but which C does. This function is called popen(). popen() creates a pipe: it launches the given program, but redirects that program's output into a new filehandle in your program. This allows you to read the output directly in your program, and do whatever you want.
Because this is not a C++ function, it doesn't integrate very nicely with C++ functions. However, Googling for "popen c++" does reveal some C++ wrappers around popen() which might work nicely.
I hope this helps.DISTRO=Arch
Registered Linux User #388732
- 06-25-2009 #5
(EDIT): Stupid me, Cabhan already provided this information...
Well, that's not entirely true, of course... When the shell runs a command pipeline, it routes the stdin/stdout of commands being run to pipes to connect them together. Clearly one could use the same sequence of system calls (i.e. pipe(), fork(), exec()) to run a program and have access to its I/O as it runs...
This basic functionality is also provided by the POSIX popen() call, which is like system() except that it gives you a file stream for a pipe that connects to either the process's input or its output (can't do both at the same time with popen(), unfortunately.)
Working with a C FILE* in C++ isn't ideal of course - unless you happen to prefer cstdio over iostream... I think if I were doing it I'd create a C++ class that goes through the equivalent system calls but creates an iostream - unless there happens to be a convenient way to wrap a FILE* as an iostream... Or if Boost provides such a thing, that would work, too.


Reply With Quote
