LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using linux commands in C++ (https://www.linuxquestions.org/questions/linux-newbie-8/using-linux-commands-in-c-805656/)

anu_123 05-03-2010 10:11 AM

Using linux commands in C++
 
How do i use/call the linux command

hdparm -i /dev/hda

in/through a C++ program?

thanks

someshpr 05-03-2010 10:19 AM

You can try the system command from <cstdlib>.

See here details.
Quote:

int system ( const char * command );

Execute system command
Invokes the command processor to execute a command. Once the command execution has terminated, the processor gives the control back to the program, returning an int value, whose interpretation is system-dependent.

The function call also be used with NULL as argument to check whether a command processor exists.
Hope this helps,

anu_123 05-03-2010 11:09 PM

#include <stdlib.h>
#include <iostream>
#include <stdio.h>

int main()
{
system("hdparm -i /dev/hda | grep -i serial");

}

i wrote the following code and it gives the output as:

[root@localhost anuradha]# ./hddid
Model=SAMSUNG SP0802N, FwRev=TK200-04, SerialNo=S00JJ20X897934
[root@localhost anuradha]#


what i need is.. i should be able to use the output further in my code...
so i dont have any handle or variable where this output is stored..
so that i can use it further..

Valery Reznic 05-03-2010 11:54 PM

Quote:

Originally Posted by anu_123 (Post 3956389)
#include <stdlib.h>
#include <iostream>
#include <stdio.h>

int main()
{
system("hdparm -i /dev/hda | grep -i serial");

}

i wrote the following code and it gives the output as:

[root@localhost anuradha]# ./hddid
Model=SAMSUNG SP0802N, FwRev=TK200-04, SerialNo=S00JJ20X897934
[root@localhost anuradha]#


what i need is.. i should be able to use the output further in my code...
so i dont have any handle or variable where this output is stored..
so that i can use it further..

man popen.

Popen will allow you to read output of executed program.
Just don't forget when you done with reading use pclose

Andrew Dufresne 05-04-2010 02:44 AM

Quote:

Originally Posted by anu_123 (Post 3956389)
what i need is.. i should be able to use the output further in my code...
so i dont have any handle or variable where this output is stored..
so that i can use it further..

Once I had to use the output of few commands in my C program. I redirected the output of the command in a file and then read that file to extract my required information.

Like, I had to check whether I am connected to other box or not. I found ping to be an easy way to check the connection. If

Quote:

ping -c 10 192.168.1.61
gives destination host unreachable, then there is some problem with the connection.

I did the following
Quote:

system( "ping -c 10 192.168.1.61 | grep -i "destination host unreachable" > file.temp ") ;
system( "wc -c file.temp > file2.temp" ) ;
Then I opened the file2.temp using fopen and read it using fgetc. If it gives zero, then we are connected to the box.

Quote:

$ ping -c 10 192.168.1.61 | grep -i "destination host unreachable" > file.temp # if we are connected then grep will not get any distination host unreachable due to which it won't write a thing on file.temp
$ wc -c file.temp # so wc will tell us that file.temp has 0 bytes
Later, in some other cases I used sed to extract my required info and redirected them to other file, which I later read with usual library functions - fopen, fscanf etc.

Admittedly, this is not a proper or an ingenious way :) Perhaps, gurus at LQ will share a proper method.


All times are GMT -5. The time now is 08:12 PM.