ReadLine

ReadLine["file"]

reads a line of text from a file and returns it as a string.

ReadLine[stream]

reads a line of text from a stream and returns it as a string.

ReadLine[proc]

reads a line of text generated by an external process and returns it as a string.

Details and Options

  • ReadLine reads until it encounters a newline, either "\n" or "\r\n".
  • ReadLine[File["file"],] is also supported.
  • ReadLine["file",] and ReadLine[File["file"],] will first open "file" for reading if it is not already open. The file will be left open until it is closed by a call to Close.
  • ReadLine is a blocking function that by default will not return until it has read a complete line of text.
  • With the option setting TimeConstraint->t, ReadLine waits at most t seconds, then returns whatever it has read by that time.
  • If ReadLine is called on a source of content that has already been exhausted, it returns EndOfFile.
  • ReadLine[src] is equivalent to ReadString[src,"\n"|"\r\n"].

Examples

open allclose all

Basic Examples  (3)

Read the first line of the file "ExampleData/strings":

Read the next line:

Both outputs are strings:

Close the file:

Start the system shell process:

Read all initial output; depending on the operating system and shell, this can result in an empty string:

Write a command into the shell:

Read the command output:

Start the system shell process:

Read all initial output; depending on the operating system and shell, this can result in an empty string:

Write two "echo" commands in the shell, then close it with "exit":

Read the shell process output:

Scope  (1)

Read the first line of a File object:

Close the file:

Wolfram Research (2014), ReadLine, Wolfram Language function, https://reference.wolfram.com/language/ref/ReadLine.html (updated 2016).

Text

Wolfram Research (2014), ReadLine, Wolfram Language function, https://reference.wolfram.com/language/ref/ReadLine.html (updated 2016).

BibTeX

@misc{reference.wolfram_2020_readline, author="Wolfram Research", title="{ReadLine}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/ReadLine.html}", note=[Accessed: 23-January-2021 ]}

BibLaTeX

@online{reference.wolfram_2020_readline, organization={Wolfram Research}, title={ReadLine}, year={2016}, url={https://reference.wolfram.com/language/ref/ReadLine.html}, note=[Accessed: 23-January-2021 ]}

CMS

Wolfram Language. 2014. "ReadLine." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/ReadLine.html.

APA

Wolfram Language. (2014). ReadLine. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReadLine.html