Solved

pipe strings into application

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS Central Authentication 1 87
Apache module 5 47
PHP website on Linux - server DNS address could not be found. 18 48
rsyslog raw message 4 15
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…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

830 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