redirect C output to shell (bash) input

This is a discussion on redirect C output to shell (bash) input within the C Programming forums, part of the General Programming Boards category; Ive been up and down google all yesterday, and found this forum, and looked through here all morning and cant ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    7

    redirect C output to shell (bash) input

    Ive been up and down google all yesterday, and found this forum, and looked through here all morning and cant find a simple answer to this:

    I want to redirect the output of some C code into a bash shell.
    Basically I want to use C to open mpg321 and then process the keypresses to control the program.
    I read that it was done by something like redirecting the output to "/dev/tty" but I havent figured out if thats even close.
    If you have a short answer it would be greatly appriciated, or if you have a link where I can read up on it Id like that too.

    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    You have a program which generates commands understood by mpg321 ?

    If so, try
    myprog | mpg321
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    7
    Ok, Im at work slacking off right now so I dont have all my wonderful books with me. But am I correct in assuming that you mean I can do something along the lines of:

    printf ("my code to pass to mpg321 here") | mpg321;

    Also, how would I open mpg321 from within the C code? Basically if you can show me a way to pass text to a shell (bash in my case) and have it be interpretted as commands/input Im sure I can figure out the rest. I am working with linux, I think I remember something from a while back when I was using Boreland C++ with DOS that you could do something like (System, "command here"); and it would be interpretted as a command in DOS.

    Thanks again!

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    popen() lets you pass commands from C to stdin of a bash script.

    If you are passing arguments to mpg321, then use either system("mpg321 arg1 arg2");
    or read up on fork(), execlp(), and waitpid().

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    7
    Awesome, thats what I was looking for. Thanks for the quick replies. Ill read up on those other ones, because its not quite working how I want it to yet. Im trying to take input from a USB device to control mpg321, I can make the program start and play a song (hard coded) but I cant pause or stop while the song is playing, linux just buffers the commands until its done with the script that play the song, then dumps all of them at once.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Ok, Im at work slacking off right now so I dont have all my wonderful books with me. But am I correct in assuming that you mean I can do something along the lines of:

    printf ("my code to pass to mpg321 here") | mpg321;
    Not quite. Salem's advice was not for use inside of the program.

    Say you have your bash shell, then enter the following into the shell:
    $ myprog | mpg321

    This will pipe your programs stdout to mpg321. (I believe it does the same with stderr, but I'm not positive on this.)
    Last edited by Zach L.; 08-23-2005 at 11:40 AM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > I believe it does the same with stderr, but I'm not positive on this
    A regular pipe just does stdout, though adding stderr is pretty easy - just read the bash manual for all the wacky syntax
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    7
    Excellent. Thanks for all the help guys. I think Im where I want to be with this now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 02:36 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  3. Need a lesson on input and output data
    By dispatch4599 in forum C++ Programming
    Replies: 16
    Last Post: 02-06-2008, 08:04 PM
  4. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 05:41 PM
  5. Multi input to Output.
    By Krullt in forum C Programming
    Replies: 3
    Last Post: 09-25-2001, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21