Solved

pipe strings into application

Posted on 2009-05-11
4
538 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now