473,569 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using stdin and/or stdout between two programs

Hi,

Using fgets() I can read from stdin and with fputs() I can write to
stdout. Now I have two programs, one writing to stdin and the other
one reading from stdin. And I would like the second program to read
the characters the first program has written to stdin, but I don't get
it how to do this.

The program which writes to stdin:

#include <stdio.h>
int main ()
{
char *string = "Hello\n";
fputs (string, stdin);
return 0;
}

The program which reads from stdin:

#include <stdio.h>
int main ()
{
char buffer [10];
fgets (buffer, 10, stdin);
printf ("%s", buffer);
return 0;
}

Is it possible at all to use such a construction to read from stdin
what an other program wrote to stdin? BTW, I'm using Linux.

Regards,
Harayasu
Nov 13 '05 #1
3 18357
Harayasu wrote:
fputs (string, stdin); ^^^^^
Replace that by stdout.
BTW, I'm using Linux.


The stdin/stdout mix-up was your problem. Test the source like this ...

[sven@zeibig stdinout]$ tail out.c
#include <stdio.h>
int main ()
{
char *string = "Hello\n";
fputs (string, stdout);
return 0;
}
[sven@zeibig stdinout]$ tail in.c
#include <stdio.h>
int main ()
{
char buffer [10];
fgets (buffer, 10, stdin);
printf ("%s", buffer);
return 0;
}
[sven@zeibig stdinout]$ gcc -Wall -O3 -pedantic -ansi -o out out.c
[sven@zeibig stdinout]$ gcc -Wall -O3 -pedantic -ansi -o in in.c
[sven@zeibig stdinout]$ ./out | ./in
Hello
[sven@zeibig stdinout]$

HTH

/Sven

--
Sven Semmler http://www.semmlerconsulting.com/
GPG Fingerprint: 72CA E26D C2A3 1FEB 7AFC 10EA F769 A9A4 937F 5E67
Nov 13 '05 #2
Harayasu wrote:

Using fgets() I can read from stdin and with fputs() I can write
to stdout. Now I have two programs, one writing to stdin and the
other one reading from stdin. And I would like the second program
to read the characters the first program has written to stdin,
but I don't get it how to do this.

The program which writes to stdin:

#include <stdio.h>
int main ()
{
char *string = "Hello\n";
fputs (string, stdin); ^^^^^
That should be "stdout"
return 0;
}

The program which reads from stdin:

#include <stdio.h>
int main ()
{
char buffer [10];
fgets (buffer, 10, stdin);
printf ("%s", buffer);
return 0;
}

Is it possible at all to use such a construction to read from
stdin what an other program wrote to stdin? BTW, I'm using Linux.


This is OS dependant, but very common. On Linux, Unix, MSDos, a
Windows command line, you would enter:

writer | reader

and reader will take its stdin from the stdout of writer.

This has nothing to do with the C language, but is elementary OS
usage. This sort of thing is fundamental to writing simple
programs and connecting them via scripts.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #3
> Now I have two programs, one writing to stdin and the other
one reading from stdin. And I would like the second program to read
the characters the first program has written to stdin, but I don't get
it how to do this.


One thing that you've probably misinterpreted is that stdin/stdout are
relative to processes. I.e., you should talk about "its" stdin and
"its" stdout when talking about a process. You can think of
stdin/stdout as a couple of connectors to the outer world given to any
running process in your system. stdin can *only* be used to input, and
stdout can *only* be used to output.

So your question (to which you find answers in other posters' replies)
should really be: how do I connect the first program's stdout to the
second program's stdin (so that what the first program outputs to its
stdout is input by the second programs from its stdin).

HTH
MC
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4708
by: Jean-Pierre Bergamin | last post by:
Dear python-Community We are forced to use a quite old simulation software that's based on Modula-2. The idea is now to let this software "talk" to the outside world over a TCP/IP network. Since the program has no possibility to use sockets or other mechanisms to send data over the network we have the idea to let python do the network...
1
2603
by: Vincent Touquet | last post by:
Hi, In a project where I have embedded Python in a C++ application, I have the need to replace what Python considers to be stdin, stdout and/or stderr. In sysmodule.c in the Python sources, I find the following lines of code: sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
1
3965
by: Harry George | last post by:
Normally the SOAP Servers are designed to take control of a port and run their own sockets via inheritance from SocktServer. But under inetd and xinetd, the port is controlled elsewhere and the service just gets the stdin/stdout. I need to configure (or tweak) one of the SOAP servers to use that connection. Has anyone done this with any...
1
2822
by: Michael McGarry | last post by:
Hi, How do I redirect stdin, stdout and stderr to a window? I am using Qt for GUI. Thanks, Michael
5
2067
by: Michael McGarry | last post by:
Hi, How can I redirect stdin/stdout to GUI widgets? Michael
3
893
by: Mike Finister | last post by:
Hi there At the moment I am writing a GUI front-end that in the background is to call some scripts on a Unix box. The GUI has to be written in Visual Basic (yes I said the VB word...sorry! :-) ) The big issue is that the command line utility that has to be used in order to access the Unix box asks for a password (you can send the user ID...
5
3546
by: Martijn Brouwer | last post by:
I am writing a unix daemon in python, so I want to close stdin, stdout and stderr. My first attempt was to the standard file descriptors using their close() methods. After closing stdout, I could not print anymore, so this seemed to work. However, later I noticed that they were not really closed. When I close them using os.close(), it did...
2
3413
momotaro
by: momotaro | last post by:
Hello! am wondring if stdin, stdout and stderr are of any help since we can do the same thing using 'printf' for example: fprintf(stdout, "%s", message); is exactely the same as: fprintf(stderr, "%s", message); and yet:
8
4585
by: subramanian100in | last post by:
The file stdio.h just contains extern FILE *stdin; extern FILE *stdout; extern FILE *stderr; When are the standard streams stdin, stdout, stderr initialiazed ? How are they initialiazed ? Kindly explain.
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.