LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 09: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, 09:16 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
'man popen'.
 
Old 08-14-2004, 09: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, 09:59 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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, 10: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 10:15 PM.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 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
Open Source Consulting | Domain Registration