Solved

pipe strings into application

Posted on 2009-05-11
4
544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

615 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