Skip to content

Commit 6084a62

Browse files
nishtha981vtjnashstevengj
authored
Adds information to redirect_std* inline docs (#49563)
Partially addresses issue #35959 Adds examples for redirecting std to a file to the markdown file --------- Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Steven G. Johnson <[email protected]>
1 parent b9dcb94 commit 6084a62

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

doc/src/manual/networking-and-streams.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Networking and Streams
22

33
Julia provides a rich interface to deal with streaming I/O objects such as terminals, pipes and
4-
TCP sockets. This interface, though asynchronous at the system level, is presented in a synchronous
5-
manner to the programmer and it is usually unnecessary to think about the underlying asynchronous
6-
operation. This is achieved by making heavy use of Julia cooperative threading ([coroutine](@ref man-tasks))
4+
TCP sockets.
5+
These objects allow data to be sent and received in a stream-like fashion, which means that data is processed sequentially as it becomes available.
6+
This interface, though asynchronous at the system level, is presented in a synchronous manner to the programmer.
7+
This is achieved by making heavy use of Julia cooperative threading ([coroutine](@ref man-tasks))
78
functionality.
89

910
## Basic Stream I/O
@@ -66,8 +67,8 @@ abcd
6667
"abcd"
6768
```
6869

69-
Note that depending on your terminal settings, your TTY may be line buffered and might thus require
70-
an additional enter before the data is sent to Julia.
70+
Note that depending on your terminal settings, your TTY ("teletype terminal") may be line buffered and might thus require an additional enter before `stdin` data is sent to Julia.
71+
When running Julia from the command line in a TTY, output is sent to the console by default, and standard input is read from the keyboard.
7172

7273
To read every line from [`stdin`](@ref) you can use [`eachline`](@ref):
7374

@@ -205,6 +206,24 @@ julia> open("hello.txt") do f
205206
"HELLO AGAIN."
206207
```
207208

209+
If you want to redirect stdout to a file
210+
211+
```# Open file for writing
212+
out_file = open("output.txt", "w")
213+
214+
# Redirect stdout to file
215+
redirect_stdout(out_file) do
216+
# Your code here
217+
println("This output goes to `out_file` via the `stdout` variable.")
218+
end
219+
220+
# Close file
221+
close(out_file)
222+
223+
```
224+
225+
Redirecting stdout to a file can help you save and analyze program output, automate processes, and meet compliance requirements.
226+
208227
## A simple TCP example
209228

210229
Let's jump right in with a simple example involving TCP sockets.
@@ -336,7 +355,6 @@ ip"74.125.226.225"
336355

337356
## Asynchronous I/O
338357

339-
340358
All I/O operations exposed by [`Base.read`](@ref) and [`Base.write`](@ref) can be performed
341359
asynchronously through the use of [coroutines](@ref man-tasks). You can create a new coroutine to
342360
read from or write to a stream using the [`@async`](@ref) macro:
@@ -418,6 +436,7 @@ close(socket)
418436
This example gives the same functionality as the previous program, but uses IPv6 as the network-layer protocol.
419437

420438
Listener:
439+
421440
```julia
422441
using Sockets
423442
group = Sockets.IPv6("ff05::5:6:7")
@@ -430,6 +449,7 @@ close(socket)
430449
```
431450

432451
Sender:
452+
433453
```julia
434454
using Sockets
435455
group = Sockets.IPv6("ff05::5:6:7")

0 commit comments

Comments
 (0)