LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-14-2004, 10:15 PM   #1
ryan.n
LQ Newbie
 
Registered: Aug 2004
Posts: 9

Rep: Reputation: 0
C function to execute a program and return the output of the program


Hello. I am making a program in C to display the song xmms is playing using xosd. The only problem I have is how to retrieve the current song. I figured the best way would be to execute
Code:
xmmsctrl title
What function can I use to execute a program in C and return the output? Please give the function name and header file.

Thank you for your time.
 
Old 08-14-2004, 10:16 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
'man popen'.
 
Old 08-14-2004, 10:48 PM   #3
ryan.n
LQ Newbie
 
Registered: Aug 2004
Posts: 9

Original Poster
Rep: Reputation: 0
Code:
FILE *f=popen("xmmsctrl title","r");
char *ch;
puts(ch);
pclose(f);
this gives me a segmentation fault.
 
Old 08-14-2004, 10:59 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
And it should. You're referencing an unitialized pointer, the behavior of which is undefined. You're lucky your program didn't send an e-mail bomb to your employer about the use of company facilities.

Try this instead:
Code:
FILE *f = popen("xmmsctrl title", "r");
// Note: should really check if f == NULL here.
char buf[1024];
fgets(buf, sizeof(buf), f)
pclose(f);
printf("Now playing: %s", buf);
 
Old 08-14-2004, 11:11 PM   #5
ryan.n
LQ Newbie
 
Registered: Aug 2004
Posts: 9

Original Poster
Rep: Reputation: 0
thanks

Last edited by ryan.n; 08-14-2004 at 11:15 PM.
 
  


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
How to execute c++ program Daz_Man2803 Programming 8 06-09-2005 12:28 PM
How do I execute a program? erick99 Linux - Software 1 10-11-2004 08:56 AM
how to check the return value of a program Sammy2ooo Linux - General 1 04-27-2004 03:55 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 11:02 AM
execute a program athenerx Programming 3 05-23-2002 05:45 PM


All times are GMT -5. The time now is 09:26 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration