readline
readline(stream=STDIN)
Read a single line of text, including a trailing newline character (if one is reached before the end of the input), from the given stream
(defaults to STDIN
),
Examples
julia> foo = readline();
abc
julia> print(foo);
"abc\n"
Reading from a file
julia> fp = open("helloworld.txt");
julia> readline(fp) # read first line
"hello\n"
julia> readline(fp) # read second line
"world\n"
-
Read a line from standard input:
julia> line = readline() Hello, World! "Hello, World!\n"
This example reads a single line of text from the standard input (
STDIN
) and assigns it to the variableline
. -
Read a line from a file:
julia> file = open("data.txt") julia> line = readline(file) "This is line 1\n"
Here, the
readline
function is used to read a line from a file named "data.txt" that has already been opened using theopen
function. -
Read a line from a custom input stream:
julia> input = IOBuffer("Custom input stream\n") julia> line = readline(input) "Custom input stream\n"
In this example, a custom input stream
input
is created usingIOBuffer
. Thereadline
function is then used to read a line from this custom input stream. - Handle an empty input line:
julia> line = readline() ""
If the input line is empty (contains no characters), the
readline
function returns an empty string.
See Also
deserialize, eachline, eof, fd, flush, IOBuffer, ismarked, isopen, isreadonly, mark, nb_available, open, pipeline, position, read, read!, readavailable, readbytes, readbytes!, readline, redirect_stderr, redirect_stdin, reset, seek, seekend, seekstart, serialize, skip, skipchars, TextDisplay, unmark, write, writemime,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.