LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-03-2010, 11:11 AM   #1
anu_123
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Rep: Reputation: 0
Question Using linux commands in C++


How do i use/call the linux command

hdparm -i /dev/hda

in/through a C++ program?

thanks
 
Old 05-03-2010, 11:19 AM   #2
someshpr
Member
 
Registered: Jul 2009
Location: CA, USA
Distribution: Debian 7, Ubuntu 14.04
Posts: 123

Rep: Reputation: 26
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,
 
1 members found this post helpful.
Old 05-04-2010, 12:09 AM   #3
anu_123
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Original Poster
Rep: Reputation: 0
#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..

Last edited by anu_123; 05-04-2010 at 12:21 AM.
 
Old 05-04-2010, 12:54 AM   #4
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 675

Rep: Reputation: 136Reputation: 136
Quote:
Originally Posted by anu_123 View Post
#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
 
Old 05-04-2010, 03:44 AM   #5
Andrew Dufresne
LQ Newbie
 
Registered: Sep 2009
Distribution: Fedroa 10
Posts: 27

Rep: Reputation: 16
Quote:
Originally Posted by anu_123 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - Software 2 07-02-2008 07:26 PM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - General 2 07-01-2008 07:53 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration