Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pipe strings into application

Posted on 2009-05-11
4
Medium Priority
?
545 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 1600 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 400 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

664 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