Solved

pipe strings into application

Posted on 2009-05-11
4
541 Views
Last Modified: 2013-12-16
I need to pipe 2 strings into an application in C

I would do something like this in bash:
echo "$username\n$password" | /path/to/my_app $arg1 $arg2 $arg3

How would I do the same in C?
0
Comment
Question by:amrox
  • 2
4 Comments
 
LVL 14

Expert Comment

by:cjl7
ID: 24355786
system('echo asdflf |/path/to/your/app')

quick 'n' dirty! (the way we like it...)

But if you treat C-apps like shell-scripts you probably should use shell-scripts instead?! ;)
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 24355806
FILE *p=popen("/path/to/my_app $arg1 $arg2 $arg3","w");
fprintf(p,"%s\n","$username\n$password" );
0
 
LVL 4

Assisted Solution

by:lhl60
lhl60 earned 100 total points
ID: 24362087
using a pipe '|' simply connect stdout with stdin on either side of the pipe,and has nothing to do with command line arguments.


So lets pretend we have 2 very simple programs: "left" and "right"

when you execute left, it only output a string to stdout (could be "hello world" :-)  )

#left
hello here is left, bye now
#

right will just read stdin and wait for you to type something, and then echo it back to you

#right
here is right:  type something> [here you type some thing]
here is right:  you typed : [echo what you typed]
#

combining them with a pipe

#left|right
here is right,  type something>
here is right,  you typed : hello here is left program
#

if as in your case the left program is echo then my_app simply read stdin to pick up the output from echo ,you can still put as many command line args on it as my_app need.


0
 
LVL 4

Expert Comment

by:lhl60
ID: 24362120
btw: REMEMBER
 my_app must be prepared to accept that is nothing on the stdin, if it was executed "stand alone" with out using a pipe
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question